package com.moduls.interfaceFunction.controller;

import java.beans.IntrospectionException;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.javassist.compiler.ast.NewExpr;
import org.apache.ibatis.javassist.expr.NewArray;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.aliyuncs.exceptions.ClientException;
import com.auth0.jwt.internal.com.fasterxml.jackson.databind.deser.Deserializers.Base;
import com.auth0.jwt.internal.org.apache.commons.lang3.ObjectUtils.Null;
import com.google.common.collect.Maps;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.moduls.data.common.AtoboUtil;
import com.moduls.interfaceFunction.alipay.config.AlipayConfig;
import com.moduls.interfaceFunction.core.HtmlUtils;
import com.moduls.interfaceFunction.core.PageModel;
import com.moduls.interfaceFunction.core.Result;
import com.moduls.interfaceFunction.core.SqlMapper;
import com.moduls.interfaceFunction.enums.ChangeTypeEnum;
import com.moduls.interfaceFunction.enums.ExportEnum;
import com.moduls.interfaceFunction.enums.FormatEnum;
import com.moduls.interfaceFunction.enums.ImportEnum;
import com.moduls.interfaceFunction.enums.MsgTypeEnum;
import com.moduls.interfaceFunction.enums.OiTypeEnum;
import com.moduls.interfaceFunction.enums.OrderTypeEnum;
import com.moduls.interfaceFunction.enums.PrintEnum;
import com.moduls.interfaceFunction.enums.RoleEnum;
import com.moduls.interfaceFunction.enums.SetTypeEnum;
import com.moduls.interfaceFunction.enums.UpdateTypeEnum;
import com.moduls.interfaceFunction.excel.export.BalanceBean;
import com.moduls.interfaceFunction.excel.export.BillBean;
import com.moduls.interfaceFunction.excel.export.DiffBean;
import com.moduls.interfaceFunction.excel.export.EntryBean;
import com.moduls.interfaceFunction.excel.export.EntryDetailBean;
import com.moduls.interfaceFunction.excel.export.EntryOut;
import com.moduls.interfaceFunction.excel.export.ExcelBean;
import com.moduls.interfaceFunction.excel.export.ItemBean;
import com.moduls.interfaceFunction.excel.export.ManagementBean;
import com.moduls.interfaceFunction.excel.export.ProfitBean;
import com.moduls.interfaceFunction.excel.export.ProfitTwelveBean;
import com.moduls.interfaceFunction.excel.export.SortingBean;
import com.moduls.interfaceFunction.excel.export.StockBean;
import com.moduls.interfaceFunction.excel.export.SupplyBean;
import com.moduls.interfaceFunction.excel.export.SupplyFuheBean;
import com.moduls.interfaceFunction.excel.export.SupplyPlayBean;
import com.moduls.interfaceFunction.excel.export.WorkerBean;
import com.moduls.interfaceFunction.excel.export.WorkerFuheBean;
import com.moduls.interfaceFunction.excel.imports.PriceTempBean;
import com.moduls.interfaceFunction.message.MessageController;
import com.moduls.interfaceFunction.model.Systemmessage;
import com.moduls.interfaceFunction.util.DateUtils;
import com.moduls.interfaceFunction.util.ExcelUtil;
import com.moduls.interfaceFunction.util.StringUtil;
import com.moduls.interfaceFunction.weixin.pay.WeixinPay;
import com.moduls.push.UPushUtils;

import freemarker.core.ParseException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


//供应商端接口
public class HxTBackBaseApi extends HxClientApi {

	//接口开始*******************************************************************
	//接口功能:后台人员登录
	
	/**
	 * huangye88 数据
	 * @throws IOException
	 * @throws SQLException
	 */
	public void huangye88_data() throws IOException, SQLException {
		//城市数据开始
		Document doc = Jsoup.connect("http://b2b.huangye88.com/region/") 
				  .data("query", "Java")   // 请求参数
				  .userAgent("I ’ m jsoup") // 设置 User-Agent 
				  .cookie("auth", "token") // 设置 cookie 
				  .timeout(5000)           // 设置连接超时时间
				  .post();                 // 使用 POST 方法访问 URL 
		
	}
	
	
	
	//Atobo 城市数据添加
	//{"mid":"1","action":"atobo_city_add"}
	public void atobo_city_add() throws IOException, SQLException {
		SqlMapper sqlMapper= new SqlMapper(sqlSessions);
		
		List<Map<String, String>> list = AtoboUtil.atoboCity();
		
		String sql = "insert into t_atobo (city_name,city_url) values ";
		
		for (Map<String, String> map : list) {
			String city_name = map.get("city_name");
			String city_url = map.get("city_url");
			sql+= "('"+city_name+"','"+city_url+"'),";
		}
		
		sql = sql.substring(0, sql.length()-1);
		
		System.out.println(sql);
		sqlMapper.insert(sql);
		
	}
	
	//Atobo 公司数据添加
	//{"mid":"1","action":"atobo_city_company_add"}
	public void atobo_city_company_add() throws IOException, SQLException {
		SqlMapper sqlMapper= new SqlMapper(sqlSessions);
		
		List<Map<String, Object>> list = new ArrayList<>();
		
		String sql = "select atobo_id,city_url from t_atobo where atobo_id =2";
		list = sqlMapper.public_list_all_sql(sql);
		
		for (Map<String, Object> map : list) {
			//String sql_insert = "insert into t_atobo_company (atobo_id,company,address,user_name,telephone,phone,website) values";
			String sql_insert = "insert into t_atobo_company (atobo_id,result) values";
			
			int atobo_id = Integer.valueOf(map.get("atobo_id").toString());
			String city_url = map.get("city_url").toString();
			//公司地址
			List<String> list_ = AtoboUtil.atoboCityCompany(city_url);
			for (String str : list_) {
				
				Map<String, String> map_info = AtoboUtil.atoboCityCompanyInfo(str);
				/*String company = map_info.get("company");
				String address = map_info.get("address");
				String zip_code = map_info.get("zip_code");
				String user_name = map_info.get("user_name");
				String telephone = map_info.get("telephone");
				String phone = map_info.get("phone");
				String website = map_info.get("website");*/
				String result = map_info.get("result");
				//sql_insert+= "("+atobo_id+",'"+company+"','"+address+"','"+zip_code+"','"+user_name+"','"+telephone+"','"+phone+"','"+website+"'),";
				sql_insert+= "("+atobo_id+",'"+result+"'),";
			}
			sql_insert = sql_insert.substring(0, sql.length()-1);
			sqlMapper.insert(sql);
		}
		
	}
	
	public void b_member_login_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		String account = obj_vars.optString("account");//登录账号
		String pwd = obj_vars.optString("pwd");//登录密码
		
		String sql = "select member_id, member_name from hx_member where account = '%s' and pwd = '%s'";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql, account, basictool.MD5(pwd)));
		
		String message = "";
		int status = 1;
		if (map != null) { //存在
			String is_forbid=sqlMapper.public_one_scale(String.format("select is_forbid from hx_member where member_id=%s ", map.get("member_id")));
			if(is_forbid!=null) {
				//账户禁用
				if(is_forbid.equals("1")) {
					message = "账户禁用";
					status = 0;
				} else {
					message = "登录成功";
				}
			}
		} else {
			message = "账号或密码错误";
			status = 0;
		}
		
		results.setResult(map);//输出：登录者信息
		results.setMessage(message);//输出：提示信息
		results.setStatus(status);//输出：状态1成功 0失败
		
	}
	
	//接口功能:管理员设置==>列表
	//{"mid":"1","action":"b_admin_list_get","member_name":"g","role_id":"","pagenum":"0","pagesize":"0"}
	public void b_admin_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String member_name = obj_vars.optString("member_name");//姓名(没有为空)
		String role_id = obj_vars.optString("role_id");//角色id(全部为空)
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT hm.member_id, hm.account, group_concat(hr.role_name) role_name, hm.member_name, hm.phone, hm.is_forbid,"
				+ "   date_format(hm.create_time,'%Y-%m-%d %H:%i:%m') create_time, if(hr.role_id = 1,1,2) as is_admin "
				+ " FROM hx_member hm "
				+ " left join hx_role_member hrm on hm.member_id = hrm.member_id "
				+ " left join hx_role hr on hrm.role_id = hr.role_id "
				+ " WHERE 1=1 and hm.is_delete = 2  ";
		if (!StringUtil.isEmpty(member_name)) {
			sql+= " and hm.member_name like '%"+member_name+"%' ";
		}
		if (!StringUtil.isEmpty(role_id)) {
			sql+= " and hr.role_id = "+role_id+" ";
		}
		sql+= " group by member_id  order by hm.create_time desc ";
		
		System.out.println(sql);
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		String sql_size = "select count(1) from hx_member hm where 1 = 1 and hm.is_delete = 2";
		int size = Integer.valueOf(sqlMapper.public_one_scale(sql_size));
		
		pageModel.total_rows = size;
		
		results.setResult(pageModel);//输出：后台人员管理列表 is_admin(是否是管理员:1是;2不是) is_forbid(是否禁用:0正常;1禁用)
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:管理员设置==>编辑查询
	//{"mid":"1","action":"b_admin_edit_get"}
	public void b_admin_edit_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int member_c_id = obj_vars.optInt("member_c_id");//操作的管理员id
		String sql = "select hm.account, hm.member_name, hm.phone from hx_member hm where hm.member_id = %s ";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql, member_c_id));
		map.put("member_id", member_id); //后台用户id
		
		//查询管理员角色
		String sqllist = "select hr.role_id, hr.role_name, "
				+ "if(((select count(1) from hx_role_member hrm where hrm.role_id = hr.role_id and hrm.member_id = %s) > 0),1,2) is_have"
				+ "  from hx_role hr order by role_id asc";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sqllist, member_c_id));
		map.put("list", list);
		
		
		results.setResult(map);//输出：用户信息 is_have 1有,2无
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:管理员设置==>编辑
	//{"mid":"1","action":"b_admin_edit_set","account":"gengek","member_name":"12345","phone":"17856985214","list":[{"role_id":"2","is_have":"1"}]}
	public void b_admin_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int member_c_id = obj_vars.optInt("member_c_id");//需要操作的管理员id
		String account = obj_vars.optString("account");//登录账号
		String pwd = obj_vars.optString("pwd");//登录密码 未修改不传
		String member_name = obj_vars.optString("member_name");//姓名
		String phone = obj_vars.optString("phone");//手机号码
		String liststr = obj_vars.optString("list");// 角色修改传值, 勾选的角色id ["1","2","3"...]
		
		
		//修改人员信息
		String sql = "update hx_member set account ='%s' ";
		if (!StringUtil.isEmpty(pwd)) {
			sql+= ", pwd = '"+basictool.MD5(pwd)+"' ";
		}
		sql+= ", member_name = '%s', phone = '%s' where member_id = %s";
		sqlMapper.update(String.format(sql, account, member_name, phone, member_c_id));
		
		//清空关系 在添加
		String sqldeleteadminrole = "delete from hx_role_member where member_id = %s";
		sqlMapper.delete(String.format(sqldeleteadminrole, member_c_id));
		
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(liststr, new TypeToken<List<String>>() {}.getType());
		for (String str : list) {
			String sqlinsertadminrole = "insert into hx_role_member (role_id,member_id) values (%s,%s)";
			sqlMapper.insert(String.format(sqlinsertadminrole, Integer.valueOf(str), member_c_id));
		}
		
		results.setMessage("编辑成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:管理员设置==>禁用+恢复
	//{"mid":"1","action":"b_admin_state_set","member_id":"1","is_forbid":"1"}
	public void b_admin_state_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int member_c_id = obj_vars.optInt("member_c_id");//操作的管理员id
		int is_forbid = obj_vars.optInt("is_forbid");//是否禁用：0正常；1禁用
		
		String sql = "update hx_member set is_forbid = %s where member_id = %s";
		sqlMapper.update(String.format(sql, is_forbid, member_c_id));
		
		results.setMessage("操作成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:管理员设置==>验证账号是否重复
	//{"mid":"1","action":"b_admin_validate_get","account":"gengek"}
	public void b_admin_validate_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String account = obj_vars.optString("account");//登录账号
		
		String message = "";
		int status = 1;
		
		//判断账号是否存在
		String sql = "select count(1) from hx_member where account = '"+account+"'";
		String sum = sqlMapper.public_one_scale(sql);
		if (Integer.valueOf(sum) <= 0) {
			message = "不存在";
		} else {
			message = "已存在";
			status = 0;
		}
		
		results.setMessage(message);//输出：提示信息
		results.setStatus(status);//输出：状态1成功 0失败
		
	}
	
	//接口功能:管理员设置==>添加
	public void b_admin_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String account = obj_vars.optString("account");//登录账号
		String pwd = obj_vars.optString("pwd");//登录密码
		String member_name = obj_vars.optString("member_name");//姓名
		String phone = obj_vars.optString("phone");//手机号码
		String liststr = obj_vars.optString("list");// 权限参数 ["1","2","3",...] 管理员的角色id
		
		//增加管理员
		Map<String, String> map = new HashMap<>();
		map.put("member_name", member_name);
		map.put("account", account);
		map.put("pwd", basictool.MD5(pwd));
		map.put("phone", phone);
		int member_id_add = sqlMapper.insert_or_update("hx_member", map, null);//新增的人员id
		
		//增加用户角色
		Gson gson = new Gson();
		List<String> list = new ArrayList<>();
		list = gson.fromJson(liststr, list.getClass());
		
		for (String str : list) {
			String sqlinsertadminrole = "insert into hx_role_member (role_id,member_id) values (%s,%s)";
			sqlMapper.insert(String.format(sqlinsertadminrole, Integer.valueOf(str), member_id_add));
		}
		
		results.setMessage("添加成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:管理员设置==>获取角色信息
	public void b_role_info_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		
		String sql = "select role_id, role_name from hx_role order by role_id asc";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		results.setResult(list);//输出：角色信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:角色设置==>列表
	//{"mid":"12","action":"b_role_list_get","role_name":"","pagenum":"0","pagesize":"0"}
	public void b_role_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String role_name = obj_vars.optString("role_name");//角色名称(没有为空)
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT hr.role_id, hr.role_name, hr.intro FROM hx_role hr WHERE 1=1 ";
		if (!StringUtil.isEmpty(role_name)) {
			sql+= " and hr.role_name like '%"+role_name+"%' ";
		}
		sql+= " order by hr.create_time desc ";
		System.out.println(sql);
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		results.setResult(pageModel);//输出：角色列表
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:角色设置==>添加
	//{"mid":"1","action":"b_role_add_set","role_name":"测试角色","intro":"测试用的角色","list":"['1','2','3']"}
	public void b_role_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String role_name = obj_vars.optString("role_name");//角色名称
		String intro = obj_vars.optString("intro");//角色描述
		String liststr = obj_vars.optString("list");//拥有的权限的菜单id ["1","2",...]
		
		Gson gson = new Gson();
		List<String> list = new ArrayList<>();
		list = gson.fromJson(liststr, list.getClass());
		
		//保存角色
		Map<String, String> data  = new HashMap<>();
		data.put("role_name", role_name);
		data.put("intro", intro);
		int role_id = sqlMapper.insert_or_update("hx_role", data, null);
		//保存角色相关权限
		for (String str : list) {
			String sqladdrolemenu = "insert into hx_role_menu (role_id, menu_id) values (%s,%s)";
			sqlMapper.insert(String.format(sqladdrolemenu, role_id, Integer.valueOf(str)));
		}
		
		results.setMessage("添加成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:角色设置==>编辑查询
	//{"mid":"1","action":"b_role_edit_get","role_id":"1"}
	public void b_role_edit_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int role_id = obj_vars.optInt("role_id");//角色id
		
		//角色信息
		String sqlrole = "select hx.role_name, hx.intro from hx_role hx where hx.role_id = %s";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sqlrole, role_id));
		//角色菜单信息
		String sqlrolemenu = "select hm.menu_id, hm.menu_name, "
				+ " if(((select count(1) from hx_role_menu hrm where hrm.menu_id = hm.menu_id and hrm.role_id = %s) = 1),1,2) is_have"
				+ "  from hx_menu hm ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sqlrolemenu, role_id));
	    
		map.put("list", list);
		
		results.setResult(map);//输出：角色信息 map 角色拥有的菜单
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:角色设置==>编辑(gengek)
	//{"mid":"1","action":"b_role_edit_set","role_id":"1","role_name":"超级管理员","intro":"管理员","list":[{"menu_id":"1","is_have":"1"},{"menu_id":"2","is_have":"1"},{"menu_id":"3","is_have":"1"}]}
	public void b_role_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String role_id = obj_vars.optString("role_id");//角色id
		String role_name = obj_vars.optString("role_name");//角色名称
		String intro = obj_vars.optString("intro");//角色说明
		String liststr = obj_vars.optString("list");// 角色关联的菜单id  ["1","2"....]
		
		//修改角色信息
		String sqlupdaterole = "update hx_role set role_name = '%s', intro = '%s' where role_id = %s";
		sqlMapper.update(String.format(sqlupdaterole, role_name, intro, role_id));
		
		//删除角色菜单
		String sqldeleterolemenu = "delete from hx_role_menu where role_id = %s";
		sqlMapper.delete(String.format(sqldeleterolemenu, role_id));
		//新增角色菜单关联
		Gson gson = new Gson();
		List<String> list = gson.fromJson(liststr, new TypeToken<List<String>>() {}.getType());
		for (String str : list) {
			String sqladdrolemenu = "insert into hx_role_menu (role_id, menu_id) values (%s,%s)";
			sqlMapper.insert(String.format(sqladdrolemenu, role_id, Integer.valueOf(str)));
		}
		
		results.setMessage("编辑成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:角色设置==>删除
	//{"token":"2267a672-279f-45d6-9f13-768e2a885ca0","uid":"12","action":"b_role_delete_set","role_id":"6"}
	public void b_role_delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int role_id = obj_vars.optInt("role_id");//角色id
		//删除角色菜单
		String sqlrolemenu = "delete from hx_role_menu where role_id = %s";
		sqlMapper.delete(String.format(sqlrolemenu, role_id));
		//删除用户角色
		String sqlmemberrole = "delete from hx_role_member where role_id = %s";
		sqlMapper.delete(String.format(sqlmemberrole, role_id));
		//删除角色
		String sqlrole = "delete from hx_role where role_id = %s";
		sqlMapper.delete(String.format(sqlrole, role_id));
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:角色设置==>获取菜单列表
	//{"mid":"1","action":"b_menu_list_get"}
	public void b_menu_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		
		String sql = "select (select hxm.menu_id from hx_menu hxm where hxm.menu_id = hm.pid) menu_parent_id,  "
				+ " (select hxm.menu_name from hx_menu hxm where hxm.menu_id = hm.pid) menu_parent_name, "
				+ " hm.menu_id, hm.menu_name from hx_menu hm where menu_level = 2 order by menu_id  ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = null;
		List<Map<String,Object>> lstMapitems = null;
		String menu_parent_id = "";
		for(Map<String,Object> mapItem: list)
		{
			if(menu_parent_id.isEmpty() || !menu_parent_id.equals(mapItem.get("menu_parent_id").toString()))
			{
				map = new HashMap<String,Object>();
				menu_parent_id = mapItem.get("menu_parent_id").toString();
				map.put("menu_parent_id", mapItem.get("menu_parent_id"));
				map.put("menu_parent_name", mapItem.get("menu_parent_name"));
				//菜单大类	
				listMap.add(map);
				//菜单子类
				lstMapitems = new ArrayList<Map<String,Object>>();
				map.put("items", lstMapitems);
			}
			
			Map<String,Object> mapMenuItem = new HashMap<String,Object>(); 
			mapMenuItem.put("menu_id", mapItem.get("menu_id"));
			mapMenuItem.put("menu_name", mapItem.get("menu_name"));
			lstMapitems.add(mapMenuItem);
		}
		
		
		results.setResult(listMap);//输出：供应商 list
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:供应商列表==>列表
	//{"mid":"1","action":"b_supply_list_get","company":"","pagenum":"0","pagesize":"0"}
	public void b_supply_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String company = obj_vars.optString("company");//供应商名称(可空)
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT p.uid, ps.contact_name, ps.company, ps.contact_phone, "
				+ " (case ps.supply_type when 1 then '客户供应商' when 2 then '库存供应商' else '库存供应商,客户供应商' end ) supply_type "
				+ " , DATE_FORMAT(p.create_time,'%Y-%m-%d') create_time, "
				+ " if(supply_type = 2,'',p.is_forbid) is_forbid, if(supply_type = 2,'',p.approve_status) approve_status   "
				+"  FROM p_user p "
				+ " inner join p_supply ps on p.uid = ps.sid "
				+"  WHERE p.utype = 2  ";
		if (!StringUtil.isEmpty(company)) {
			sql+= " and ps.company like '%"+company+"%' ";
		}
		sql+= " order by p.create_time desc ";
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		System.out.println(sql);
		
		results.setResult(pageModel);//输出：供应商 list
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
				
	}
	
	
	//接口功能:供应商列表==>添加
	//{"mid":"1","action":"b_supply_add_set","account":"acfion","pwd":"123456789","supply_type":"3","contact_name":"大大人","company":"江夏区供应商","contact_phone":"17698745235","is_forbid":"0","address":"江夏区"}
	public void b_supply_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String pwd = obj_vars.optString("pwd");//登录密码
		String supply_type = obj_vars.optString("supply_type");//supply_type 供应商类型(1客户供应商2库存供应商3客户供应商,库存供应商)
		String contact_name = obj_vars.optString("contact_name");//联系人姓名
		String company = obj_vars.optString("company");//供应商名称
		String contact_phone = obj_vars.optString("contact_phone");//联系方式
		
		String is_forbid = obj_vars.optString("is_forbid");//是否禁用
		String address = obj_vars.optString("address");//地址
		
		//p_user
		Map<String, String> mapuser = new HashMap<>();
		mapuser.put("phone", contact_phone);
		mapuser.put("pwd", basictool.MD5(pwd));
		mapuser.put("is_forbid", is_forbid);
		mapuser.put("utype", "2");//供应商
		int uid = sqlMapper.insert_or_update("p_user", mapuser, null);
		
		//p_supply
		Map<String, String> mapsupply = new HashMap<>();
		mapsupply.put("sid", String.valueOf(uid));
		mapsupply.put("contact_name", contact_name);
		mapsupply.put("supply_type", supply_type);
		mapsupply.put("contact_phone", contact_phone);
		mapsupply.put("company", company);
		mapsupply.put("address", address);
		sqlMapper.insert_or_update("p_supply", mapsupply, null);
		
		results.setMessage("添加成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:供应商列表==>查看
	//"mid":"1","action":"b_supply_see_get","uid":"4"}
	public void b_supply_see_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String uid = obj_vars.optString("uid");//供应商id
		
		//查看供应商信息
		String sql = "select IFNULL(p.nick_name,insert(p.phone, 4, 4, '****')) nick_name, ps.company, p.phone, DATE_FORMAT(p.create_time,'%Y-%m-%d') create_time, ps.address "
				+ " from p_user p "
				+ " inner join p_supply ps on p.uid = ps.sid where p.uid = "+uid+"";
		Map<String, Object> map = sqlMapper.public_one_detail(sql);
		
		results.setResult(map);//输出：供应商 信息 
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:供应商列表==>编辑
	public void b_supply_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String uid = obj_vars.optString("uid");//供应商id
		String contact_name = obj_vars.optString("contact_name");//联系人姓名
		String company = obj_vars.optString("company");//供应商名称
		String contact_phone = obj_vars.optString("contact_phone");//供应商联系电话
		String address = obj_vars.optString("address");//供应商地址
		
		String sql = "update p_supply set contact_name = '%s', company = '%s', contact_phone = '%s', address = '%s' where sid = %s";
		sqlMapper.update(String.format(sql, contact_name, company, contact_phone, address, uid));
		
		results.setMessage("编辑成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:供应商列表==>客户供应商查看
	//"mid":"1","action":"b_supply_kehu_see_get","uid":"21"}
	public void b_supply_kehu_see_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String uid = obj_vars.optString("uid");//供应商id
		
		//查看供应商信息
		String sql = "select p.uid, IFNULL(p.nick_name,insert(p.phone, 4, 4, '****')) nick_name, ps.company, p.phone, DATE_FORMAT(p.create_time,'%Y-%m-%d') create_time, ps.address "
				+ " ,p.is_forbid ,p.approve_status from p_user p inner join p_supply ps on p.uid = ps.sid where p.uid = "+uid+" ";
		Map<String, Object> map = sqlMapper.public_one_detail(sql);
		
		String sqlapprove = "select IFNULL(approve_opinion,'') approve_opinion, IFNULL(DATE_FORMAT(create_time,'%Y-%m-%d %H:%i'),'') approve_time from hx_approve "
				+ " where approve_type = 100 and ref_id = "+uid+" and is_last = 1";
		Map<String, Object> map2 = sqlMapper.public_one_detail(sqlapprove);
		if (map2 == null) {
			map.put("approve_opinion", "");
			map.put("approve_time", "");
		} else {
			map.put("approve_opinion", map2.get("approve_opinion"));
			map.put("approve_time", map2.get("approve_time"));
		}
		
		results.setResult(map);//输出：供应商 信息 
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	
	//接口功能:供应商列表==>客户供应商编辑
	public void b_supply_kehu_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String uid = obj_vars.optString("uid");//供应商id
		String contact_name = obj_vars.optString("contact_name");//联系人姓名
		String company = obj_vars.optString("company");//供应商名称
		String contact_phone = obj_vars.optString("contact_phone");//供应商联系电话
		String address = obj_vars.optString("address");//供应商地址
		String pwd = obj_vars.optString("pwd");//登录密码(不修改就不传)
		int is_forbid = obj_vars.optInt("is_forbid");//是否禁用
		
		//修改p_user
		String sqluser = "update p_user set is_forbid = "+is_forbid+" ";
		if (!StringUtil.isEmpty(pwd)) {
			sqluser+= " ,pwd = '"+basictool.MD5(pwd)+"' ";
		}
		sqluser+= " where uid = "+uid+" ";
		sqlMapper.update(sqluser);
		//修改p_subbly
		String sql = "update p_supply set contact_name = '%s', company = '%s', contact_phone = '%s', address = '%s' where sid = %s";
		sqlMapper.update(String.format(sql, contact_name, company, contact_phone, address, uid));
		
		results.setMessage("编辑成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:供应商列表==>审核
	//{"mid":"1","action":"b_supply_examine_set","uid":"4","approve_status":"3","approve_opinion":"营业执照不正常","member_id":"1"}
	public void b_supply_examine_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int uid = obj_vars.optInt("uid");//供应商id
		int approve_status = obj_vars.optInt("approve_status");//2已审核；3已驳回
		String approve_opinion = obj_vars.optString("approve_opinion");//审核意见
		
		//修改供应商状态
		String sql = "update p_user set approve_status = %s where uid = %s";
		sqlMapper.update(String.format(sql, approve_status, uid));
		
		//更新之前的审核
		String sqlupdate = "update hx_approve set is_last = 0 where ref_id = %s";
		sqlMapper.update(String.format(sqlupdate, uid));
		//新增审核
		String sqladd = "insert into hx_approve (approve_status, approve_opinion, "
				+ "member_id, approve_type, ref_id, is_last) values (%s,'%s',%s,100,%s,1)";
		sqlMapper.insert(String.format(sqladd, approve_status, approve_opinion, member_id, uid));
		
		results.setMessage("审核成功");//输出：审核提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:供应商列表==>禁用or恢复
	//{"mid":"1","uid":"12","action":"b_supply_status_set","is_forbid":"1"}
	public void b_supply_status_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int uid = obj_vars.optInt("uid");//供应商id
		int is_forbid = obj_vars.optInt("is_forbid");//是否禁用：0正常；1禁用
		
		//修改供应商状态
		String sql = "update p_user set is_forbid = %s where uid = %s";
		sqlMapper.update(String.format(sql, is_forbid, uid));
		
		results.setMessage("修改成功");//输出：状态修改提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	
	//接口功能:供应商列表==>供应商品信息列表
	//{"mid":"1","uid":"4","action":"b_supply_item_get","is_forbid":"1","pagenum":"0","pagesize":"0"}
	public void b_supply_item_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int uid = obj_vars.optInt("uid");//供应商id
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT psi.supply_item_id, (select pcp.catalog_name from p_catalog pcp where pcp.catalog_id = pc.pid) catalog_name_parent, pc.catalog_name, "
				+" pi.item_name, ps.val_names, pi.unit, psi.purchase_price"
				+ " FROM p_supply_item psi "
				+" inner join p_catalog pc on psi.catalog_id = pc.catalog_id "
				+" inner join p_item pi on psi.item_id = pi.item_id  "
				+" inner join p_sku ps on ps.sku_id = psi.sku_id "
				+" WHERE psi.sid = %s and pi.is_delete = 2 order by psi.create_time desc "; //商品未删除的
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(String.format(sql, uid), pagenum);
		System.out.println(String.format(sql, uid));
		
		results.setResult(pageModel);//输出：供应商商品列表
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:供应商列表==>供应商商品信息新增查询
	//{"mid":"1","uid":"4","action":"b_supply_item_add_get","catalog_id":"13"}
	public void b_supply_item_add_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int catalog_id = obj_vars.optInt("catalog_id");//商品分类id
		
		String sql = "select ps.sku_id,ps.item_id,pi.item_name,ps.val_names, pi.unit from p_sku ps  "
				+" inner join p_item pi on pi.item_id = ps.item_id  "
				+"  where ps.is_delete = 2 and pi.is_delete = 2 and ps.item_id in (select item_id from p_item where catalog_id = %s) ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sql, catalog_id));
		
		results.setResult(list);//输出：商品子类规格信息列表
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:供应商列表==>供应商商品信息新增
	//{"mid":"1","uid":"4","action":"b_supply_item_add_set","catalog_id":"13","list":[{"item_id":"2","sku_id":"1","purchase_price":"5200"},{"item_id":"3","sku_id":"7","purchase_price":"19900"}]}
	public void b_supply_item_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int catalog_id = obj_vars.optInt("catalog_id");//商品分类id
		int uid = obj_vars.optInt("uid");//供应商id
		String liststr = obj_vars.optString("list");//[{"item_id":"1","sku_id":"2","purchase_price":"1000"},{}...] 定价列表
		
		//添加供应商菜品价格
		Gson gson = new Gson();
		List<Map<String, String>> list = gson.fromJson(liststr, new TypeToken<List<Map<String, String>>>() {}.getType());
		for (Map<String, String> map : list) {
			String sql = "insert into p_supply_item (sid,catalog_id,item_id,sku_id,purchase_price) values (%s,%s,%s,%s,%s)";
			sqlMapper.insert(String.format(sql, uid,catalog_id,map.get("item_id"),map.get("sku_id"),map.get("purchase_price")));
		}
		
		results.setMessage("保存成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:供应商列表==>供应商商品信息编辑查询
	public void b_supply_item_edit_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int supply_item_id = obj_vars.optInt("supply_item_id");//id
		
		String sql = "select psi.supply_item_id, (select catalog_name from p_catalog where catalog_id = pc.pid) catalog_parent_name, pc.catalog_name, "
				+ " pi.item_name, pi.unit, (select val_names from p_sku where sku_id = psi.sku_id) val_names, psi.purchase_price "
				+ "  from p_supply_item psi "
				+ " inner join p_catalog pc on pc.catalog_id = psi.catalog_id "
				+ " inner join p_item pi on pi.item_id = psi.item_id "
				+ " where psi.supply_item_id = %s ";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql, supply_item_id));
		
		
		results.setResult(map);//输出:提示信息
		results.setMessage("查询成功");//输出：提示信息 
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:供应商列表==>供应商商品信息编辑
	public void b_supply_item_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int supply_item_id = obj_vars.optInt("supply_item_id");//id
		String purchase_priceve  = obj_vars.optString("purchase_price");//价格
		
		String sql = "update p_supply_item set purchase_price = %s where supply_item_id = %s";
		sqlMapper.update(String.format(sql, purchase_priceve, supply_item_id));
		
		results.setMessage("编辑成功");//输出：提示信息 
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:供应商列表==>供应商商品信息删除
	public void b_supply_item_delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int supply_item_id = obj_vars.optInt("supply_item_id");//id
		
		String sql = "delete from p_supply_item where supply_item_id = %s";
		sqlMapper.delete(String.format(sql, supply_item_id));
		
		results.setMessage("编辑成功");//输出：提示信息 
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==>列表
	//{"mid":"1","action":"b_customer_list_get","company":"","salesman_id":"4","deliver_id":"2","service_id":"3","price_temp_id":"1","is_allow_price_zero":"0","pagenum":"0","pagesize":"0"}
	public void b_customer_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String kehu_company = obj_vars.optString("kehu_company"); //客户名称
		String salesman_id = obj_vars.optString("salesman_id");//业务员id
		String deliver_id = obj_vars.optString("deliver_id");//配送员id
		String service_id = obj_vars.optString("service_id");//客服id
		String price_temp_id = obj_vars.optString("price_temp_id");//价格模板id
		String is_allow_price_zero = obj_vars.optString("is_allow_price_zero");//订单项为零是否可以的提交 0不可以 1可以
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT pc.cid, pc.uid, pc.company kehu_company, pc.contact_name, pc.contact_phone, "
				+ "DATE_FORMAT(pc.create_time,'%Y-%m-%d') create_time, ppt.template_name, is_allow_price_zero "
				+ "  , hxsa.member_name salesman_name, hxse.member_name service_name, hxde.member_name deliver_name "
				+ " FROM p_customer pc "
				+ " left join p_price_temp ppt on pc.price_temp_id = ppt.price_temp_id "
				+ " left join hx_member hxsa on hxsa.member_id = pc.salesman_id "
				+ " left join hx_member hxse on hxse.member_id = pc.service_id"
				+ " left join hx_member hxde on hxde.member_id = pc.deliver_id"
				+ " WHERE 1=1 ";
		if (!StringUtil.isEmpty(kehu_company)) {//不为空
			sql+= " and pc.company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(salesman_id)) {
			sql+= " and pc.salesman_id = "+salesman_id+" ";
		}
		if (!StringUtil.isEmpty(deliver_id)) {
			sql+= " and pc.deliver_id = "+deliver_id+" ";
		}
		if (!StringUtil.isEmpty(service_id)) {
			sql+= " and pc.service_id = "+service_id+" ";
		}
		if (!StringUtil.isEmpty(price_temp_id)) {
			sql+= " and pc.price_temp_id = "+price_temp_id+" ";
		}
		if (!StringUtil.isEmpty(is_allow_price_zero)) {
			sql+= " and pc.is_allow_price_zero = "+is_allow_price_zero+" ";
		}
		sql+= " order by pc.create_time desc  ";
		System.out.println(sql);
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		
		results.setResult(pageModel);//输出:客户列表信息 salesman_name 业务员  deliver_name  配送员 service_name 客服
		results.setMessage("查询成功");//输出：提示信息 
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==>下拉数据
	//{"mid":"1","action":"b_customer_down_get"}
	public void b_customer_down_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		
		//业务员
		String sqlexamine = "select hm.member_id, hm.member_name, hm.phone from hx_member hm "
				+ " inner join hx_role_member hrm on hm.member_id = hrm.member_id where hrm.role_id = %s order by hm.member_id asc ";
		List<Map<String, Object>> examine = sqlMapper.public_list_all_sql(String.format(sqlexamine, RoleEnum.salesman.getId()));
		//客服
		String sqlservice = "select hm.member_id, hm.member_name, hm.phone from hx_member hm "
				+ " inner join hx_role_member hrm on hm.member_id = hrm.member_id where hrm.role_id = %s order by hm.member_id asc ";
		List<Map<String, Object>> service = sqlMapper.public_list_all_sql(String.format(sqlservice, RoleEnum.service.getId()));
		//配送员
		String sqldistribution = "select hm.member_id, hm.member_name, hm.phone from hx_member hm "
				+ " inner join hx_role_member hrm on hm.member_id = hrm.member_id where hrm.role_id = %s order by hm.member_id asc ";
		List<Map<String, Object>> distribution = sqlMapper.public_list_all_sql(String.format(sqldistribution, RoleEnum.deliver.getId()));
		//价格模板
		String sqltemplate = "select price_temp_id, template_name from p_price_temp  order by create_time desc";
		List<Map<String, Object>> template = sqlMapper.public_list_all_sql(sqltemplate);
		
		Map<String, Object> map = new HashMap<>();
		map.put("examine", examine);
		map.put("service", service);
		map.put("distribution", distribution);
		map.put("template", template);
		
		results.setResult(map);//输出:客户列表下拉数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==根据角色id 获取角色用户
	//{"mid":"1","action":"b_customer_role_member_list_get","role_id":"2"}
	public void b_customer_role_member_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int role_id = obj_vars.optInt("role_id");//角色id
		
		String sql = "select hm.member_id, hm.member_name, hm.phone from hx_member hm "
				+ " inner join hx_role_member hrm on hm.member_id = hrm.member_id where hrm.role_id = %s order by hm.member_id asc ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sql, role_id));
		
		results.setResult(list);//输出:角色下的用户信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==价格模板列表
	//{"mid":"1","action":"b_customer_price_temp_get"}
	public void b_customer_price_temp_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String template_name = obj_vars.optString("template_name");//模板名称(可空)
		
		String sql = "select price_temp_id, template_name, http_url, url from p_price_temp where 1 = 1 ";
		if (!StringUtil.isEmpty(template_name)) {
			sql+= " template_name like '%"+template_name+"%' ";
		}
		sql+= " order by create_time desc ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		results.setResult(list);//输出:价格模板信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==价格模板查看
	public void b_custimer_price_temp_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int price_temp_id = obj_vars.optInt("price_temp_id");//模板id
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//小类id
		String item_name = obj_vars.optString("item_name");//商品名称
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT pptd.price_temp_detail_id, "
				+ " (select pca.catalog_name from p_catalog pca where pca.catalog_id = pc.pid)"
				+ "  catalog_parent_name, pc.pid catalog_parent_id, pc.catalog_id, pc.catalog_name, pi.item_name, "
				+ " psku.val_names,pi.unit, pptd.customer_price "
				+ " FROM p_price_temp_detail pptd"
				+ " left join p_item pi on pi.item_id = pptd.item_id "
				+ " left join p_catalog pc on pc.catalog_id = pi.catalog_id "
				+ " left join p_sku psku on psku.sku_id = pptd.sku_id"
				+ " WHERE pptd.price_temp_id = "+price_temp_id+"   ";
		
		if (!StringUtil.isEmpty(catalog_parent_id)) {
			sql+= " and pc.pid = "+catalog_parent_id+" ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and pc.catalog_id = "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and pi.item_name like '%"+item_name+"%' ";
		}
		
		sql+=" order by pptd.price_temp_detail_id ";
		
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		results.setResult(pageModel);//输出:价格模板信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==>价格模板删除
	public void b_custimer_price_temp_delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int price_temp_id = obj_vars.optInt("price_temp_id");//模板id
		
		String message = "删除成功";
		int status = 1;
		
		String sql_count = "select count(1) from p_price_temp";
		String count = sqlMapper.public_one_scale(sql_count);
		
		if (Integer.valueOf(count) <= 1) {
			message = "最后一个模板不能删除";
			status = 0;
		} else {
			//删除主表
			String sql_delete = "delete from p_price_temp where price_temp_id = %s";
			sqlMapper.delete(String.format(sql_delete, price_temp_id));
			
			//删除子表
			String sql_delete_detail = "delete from p_price_temp_detail where price_temp_id = %s";
			sqlMapper.delete(String.format(sql_delete_detail, price_temp_id));
			
		}
		
		results.setMessage(message);//输出：提示信息
		results.setStatus(status);//输出：状态1成功 0失败
	}
	
	//接口功能:客户列表==>查看
	//{"mid":"1","action":"b_customer_info_get","cid":"2"}
	public void b_customer_info_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int cid = obj_vars.optInt("cid");//客户id
		
		String sql = "select pc.cid, pc.company, pc.contact_name, pc.contact_phone, pc.address, pc.salesman_id, hxsa.member_name salesman_name, "
				+ "  hxsa.phone salesman_phone, pc.service_id, hxse.member_name service_name, hxse.phone service_phone, pc.deliver_id, hxde.member_name deliver_name, "
				+ "  hxde.phone deliver_phone , ppt.template_name,ppt.price_temp_id, hbf.bill_format_name ,"
				+ " pc.bill_format_type, IFNULL(pu.nick_name,insert(pu.phone, 4, 4, '****')) nick_name  "
				+ " from p_customer pc  "
				+ " left join p_price_temp ppt on pc.price_temp_id = ppt.price_temp_id "
				+ " left join hx_member hxsa on hxsa.member_id = pc.salesman_id  "
				+ " left join hx_member hxse on hxse.member_id = pc.service_id "
				+ " left join hx_member hxde on hxde.member_id = pc.deliver_id  "
				+ " left join p_user pu on pc.uid = pu.uid  "
				+ " left join hx_bill_format hbf on pc.bill_format_type = hbf.bill_format_type where cid = %s ";
		System.out.println(String.format(sql, cid));
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql, cid));
		
		
		results.setResult(map);//输出:客户信息
		results.setMessage("查询成功");//输出：提示信息f
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==>编辑
	//{"mid":"1","action":"b_customer_edit_get","cid":"2"}
	public void b_customer_edit_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int cid = obj_vars.optInt("cid");//客户id
		String kehu_company = obj_vars.optString("kehu_company"); //客户名称
		String contact_name = obj_vars.optString("contact_name"); //联系人
		String contact_phone = obj_vars.optString("contact_phone");//联系电话
		String address = obj_vars.optString("address");//联系地址
		int salesman_id = obj_vars.optInt("salesman_id");//业务员id
		int service_id = obj_vars.optInt("service_id");//客服id
		int deliver_id = obj_vars.optInt("deliver_id");//配送员id
		int price_temp_id = obj_vars.optInt("price_temp_id");//模板id
		
		String sql = "update p_customer set company = '%s',contact_name = '%s',contact_phone = '%s',address = '%s',"
				+ " salesman_id = %s, service_id = %s, deliver_id = %s, price_temp_id = %s where cid = %s";
		sqlMapper.update(String.format(sql, kehu_company,contact_name,contact_phone,
				address,salesman_id,service_id,deliver_id,price_temp_id,cid));
		
		results.setMessage("编辑成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	
	//接口功能:验证电话是否已使用
	//{"mid":"1","action":"b_phone_get","company":"梦飞翔"}
	public void b_phone_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		String message = "";
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String phone = obj_vars.optString("phone"); //手机号码
		
		int is_repeat = 0;
		
		//电话号码 不能重复
		String sql_phone = " select count(1) from p_user where phone = '"+phone+"' ";
		int num =  Integer.valueOf(sqlMapper.public_one_scale(sql_phone));
		if (num >= 1) {
			is_repeat = 1;
			message = "号码已存在";
		} else {
			is_repeat = 2;
			message = "号码可用";
		}
		
		results.setMessage(message);//输出：提示信息f
		results.setStatus(is_repeat);//输出：状态1成功 0失败
		
	}
	
	//接口功能:编辑验证电话是否已使用
	//{"mid":"1","action":"b_phone_user_get","company":"梦飞翔"}
	public void b_phone_user_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		String message = "";
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String phone = obj_vars.optString("phone"); //手机号码
		String uid = obj_vars.optString("uid");//id
		
		int is_repeat = 0;
		
		//电话号码 不能重复
		String sql_phone = " select count(1) from p_user where uid != "+uid+" and  phone = '"+phone+"' ";
		int num =  Integer.valueOf(sqlMapper.public_one_scale(sql_phone));
		if (num >= 1) {
			is_repeat = 1;
			message = "号码已存在";
		} else {
			is_repeat = 2;
			message = "号码可用";
		}
		
		results.setMessage(message);//输出：提示信息f
		results.setStatus(is_repeat);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==>添加==>验证客户名称是否重复
	//{"mid":"1","action":"b_customer_company_get","company":"梦飞翔"}
	public void b_customer_company_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		String message = "";
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String kehu_company = obj_vars.optString("kehu_company"); //客户名称
		
		int is_repeat = 0;
		
		//客户名称 不能重复
		String sqlcompany = " select count(1) from p_customer where company = '"+kehu_company+"' ";
		int num =  Integer.valueOf(sqlMapper.public_one_scale(sqlcompany));
		if (num >= 1) {
			is_repeat = 1;
			message = "名称已存在";
		} else {
			is_repeat = 2;
			message = "该名称可用";
		}
		
		results.setMessage(message);//输出：提示信息f
		results.setStatus(is_repeat);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==>供应商下拉数据
	//{"mid":"1","action":"b_supply_down_get"}
	public void b_supply_down_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		
		String sql = "select sid, company from p_supply order by sid ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		results.setResult(list);//输出:供应商下拉数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==>添加
	//{"mid":"1","action":"b_customer_info_add_set","company":"2","contact_name":"2","contact_phone":"2","address":"2","salesman_id":"2","service_id":"2","deliver_id":"2","price_temp_id":"2","uid":"2"}
	public void b_customer_info_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String kehu_company = obj_vars.optString("kehu_company"); //客户名称
		String contact_name = obj_vars.optString("contact_name"); //联系人
		String contact_phone = obj_vars.optString("contact_phone");//联系电话
		String address = obj_vars.optString("address");//联系地址
		int salesman_id = obj_vars.optInt("salesman_id");//业务员id
		int service_id = obj_vars.optInt("service_id");//客服id
		int deliver_id = obj_vars.optInt("deliver_id");//配送员id
		int price_temp_id = obj_vars.optInt("price_temp_id");//模板id
		
		String sql = "insert into p_customer (company,contact_name,contact_phone,address,salesman_id,service_id,deliver_id,price_temp_id) "
				+ " values ('%s','%s','%s','%s',%s,%s,%s,%s) ";
		sqlMapper.insert(String.format(sql, kehu_company,contact_name,contact_phone,address,salesman_id,service_id,deliver_id,price_temp_id));
		
		results.setMessage("添加成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==>获取分类信息(大小类一起)
	//{"mid":"1","action":"b_catalog_info_get"}
	public void b_catalog_info_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		
		String sql = "select ifnull(pcp.catalog_id,0) catalog_parent_id , pcp.catalog_name catalog_parent_name, pc.catalog_id, pc.catalog_name from p_catalog pc "
				+ " inner join p_catalog pcp on pcp.catalog_id = pc.pid order by pcp.ordid is null, pcp.catalog_id, pc.ordid is null, pc.catalog_id ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = null;
		List<Map<String,Object>> lstMapitems = null;
		String catalog_parent_id = "";
		for(Map<String,Object> mapItem: list) {
			if (mapItem.get("catalog_parent_id").toString().equals("0")) {
				map = new HashMap<String,Object>();
				map.put("catalog_parent_id", mapItem.get("catalog_id"));
				map.put("catalog_parent_name", mapItem.get("catalog_name"));
				listMap.add(map);
				continue;
			}
			
			if(catalog_parent_id.isEmpty() || !catalog_parent_id.equals(mapItem.get("catalog_parent_id").toString()))
			{
				map = new HashMap<String,Object>();
				catalog_parent_id = mapItem.get("catalog_parent_id").toString();
				map.put("catalog_parent_id", mapItem.get("catalog_parent_id"));
				map.put("catalog_parent_name", mapItem.get("catalog_parent_name"));
				//订单数据
				listMap.add(map);
				//订单里面的商品
				lstMapitems = new ArrayList<Map<String,Object>>();
				map.put("items", lstMapitems);
				
			}
			
			Map<String,Object> mapCatalogItem = new HashMap<String,Object>(); 
			mapCatalogItem.put("catalog_id", mapItem.get("catalog_id"));
			mapCatalogItem.put("catalog_name", mapItem.get("catalog_name"));
			lstMapitems.add(mapCatalogItem);
			
		}
		
		results.setResult(listMap);//输出:分类信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	
	
	//接口功能:客户列表==>自动派单设置查询
	//{"mid":"1","action":"b_customer_supply_get","customer_id":"4"}
	public void b_customer_supply_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int customer_id = obj_vars.optInt("customer_id");//客户id
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//小类id
		String company = obj_vars.optString("company"); //供应商名称(可空)
		
		String sql = "select pcs.customer_supply_id, (select pcp.catalog_name from p_catalog pcp where pcp.catalog_id = pc.pid ) catalog_parent_name, "
				+ " pc.catalog_name, ps.company, pcs.is_default from p_customer_supply pcs "
				+ " left join p_catalog pc on pcs.catalog_id = pc.catalog_id "
				+ " left join p_supply ps on pcs.sid = ps.sid "
				+ " where pcs.cid = "+customer_id+"  ";
		if (!StringUtil.isEmpty(catalog_parent_id)) {
			sql+= " and pc.pid = "+catalog_parent_id+" ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and pc.catalog_id = "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(company)) {
			sql+= " and ps.company like '%"+company+"%' ";
		}
		
		sql+= " order by pcs.create_time desc ";
		System.out.println(sql);
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		results.setResult(list);//输出:自动派单设置信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==>自动派单设置添加
	//{"mid":"1","action":"b_customer_supply_add_set","cid":"7","catalog_id":"14","sid":"12","is_default":"1"}
	public void b_customer_supply_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int cid = obj_vars.optInt("cid");//客户id
		int catalog_id = obj_vars.optInt("catalog_id");//小类id
		int sid = obj_vars.optInt("sid");//供应商id
		int is_default = obj_vars.optInt("is_default");//是否默认派单 0非默认1默认
		
		String sql= "insert into p_customer_supply (cid,catalog_id,sid,is_default) values (%s,%s,%s,%s)";
		sqlMapper.insert(String.format(sql, cid,catalog_id,sid,is_default));
		
		results.setMessage("添加成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==>自动派单设置编辑查询
	//{"mid":"1","action":"b_customer_supply_edit_get","customer_supply_id":"1"}
	public void b_customer_supply_edit_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int customer_supply_id = obj_vars.optInt("customer_supply_id");//设置id
		
		String sql = "select pcs.customer_supply_id, (select catalog_name from p_catalog where catalog_id = pc.pid) catalog_parent_name, pc.pid catalog_parent_id, "
				+ " pc.catalog_name, pc.catalog_id , ps.company, ps.sid, pcs.is_default "
				+ "  from p_customer_supply pcs inner join p_catalog pc on pcs.catalog_id = pc.catalog_id "
				+ " inner join p_supply ps on ps.sid = pcs.sid where pcs.customer_supply_id = "+customer_supply_id+" ";
		Map<String, Object> map = sqlMapper.public_one_detail(sql);
		
		results.setResult(map);//输出:派单信息
		results.setMessage("编辑成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==>自动派单设置编辑
	//{"mid":"1","action":"b_customer_supply_edit_set","cid":"7","catalog_id":"14","sid":"12","is_default":"1"}
	public void b_customer_supply_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int customer_supply_id = obj_vars.optInt("customer_supply_id");//设置id
		int catalog_id = obj_vars.optInt("catalog_id");//小类id
		int sid = obj_vars.optInt("sid");//供应商id
		int is_default = obj_vars.optInt("is_default");//是否默认派单 0非默认1默认
		
		String sql = "update p_customer_supply set catalog_id = %s, sid = %s, is_default = %s where customer_supply_id = %s";
		sqlMapper.update(String.format(sql, catalog_id,sid,is_default,customer_supply_id));
		
		results.setMessage("编辑成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:下单用户管理==>列表
	//{"mid":"1","action":"b_user_liist_get","nick_name":"王","is_forbid":"0","approve_status":"2","brand_type":"1","pagenum":"0","pagesize":"0"}
	//{"mid":"1","action":"b_user_liist_get","pagenum":"0","pagesize":"0"}
	public void b_user_liist_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String nick_name = obj_vars.optString("nick_name");//下单用户姓名(可空)
		String is_forbid = obj_vars.optString("is_forbid"); //用户状态 是否禁用：0正常；1禁用(可空)
		String approve_status = obj_vars.optString("approve_status"); //账户审核状态：1未审核；2已审核；3已驳回(可空)
		String brand_type = obj_vars.optString("brand_type");//品牌id (可空)
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT pu.uid, IFNULL(pu.nick_name,insert(pu.phone, 4, 4, '****')) nick_name, pu.phone, pb.brand_name, DATE_FORMAT(pu.create_time,'%Y-%m-%d') create_time, pu.is_forbid, pu.approve_status "
				+ "  FROM p_user pu left join p_brand pb on pu.brand_type = pb.brand_type "
				+ " WHERE pu.utype = 1  ";
		if (!StringUtil.isEmpty(nick_name)) {
			sql+=" and pu.nick_name like '%"+nick_name+"%' ";
		}
		if (!StringUtil.isEmpty(is_forbid)) {
			sql+= " and pu.is_forbid = "+is_forbid+" ";
		}
		if (!StringUtil.isEmpty(approve_status)) {
			sql+= " and pu.approve_status = "+approve_status+" ";
		}
		if (!StringUtil.isEmpty(brand_type)) {
			sql+= " and pb.brand_type = "+brand_type+" ";
		}
		sql+= " order by pu.create_time desc ";
		System.out.println(sql);
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		results.setResult(pageModel);//输出:客户信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:下单用户管理==>禁用+恢复
	//{"mid":"1","action":"b_user_state_set","uid":"1","is_forbid":"1"}
	public void b_user_state_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int uid = obj_vars.optInt("uid");//用户id
		int is_forbid = obj_vars.optInt("is_forbid");//是否禁用：0正常；1禁用
		
		String sql = "update p_user set is_forbid = %s where uid = %s";
		sqlMapper.update(String.format(sql, is_forbid, uid));
		
		results.setMessage("操作成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	
	
	//接口功能:下单用户管理==>获取未绑定用户的客户下拉数据
	//{"mid":"1","action":"b_customer_get"}
	public void b_customer_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(可空)
		
		String sql = "select pc.cid, pc.company from p_customer pc where uid is null  ";
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and pc.company like '%"+kehu_company+"%' ";
		}
		sql+= " order by pc.cid ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		results.setResult(list);//输出：下拉用户选择 数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:下单用户管理==>获取品牌下拉数据
	//{"mid":"1","action":"b_brand_get"}
	public void b_brand_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		
		String sql = "select pb.brand_type, pb.brand_name from p_brand pb order by pb.brand_type ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		results.setResult(list);//输出：品牌下拉数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:下单用户管理==>添加
	//{"mid":"1","action":"b_user_add_set","nick_name":"李大伟","phone":"17985471235","pwd":"123456","is_forbid":"0","brand_type":"1"}
	public void b_user_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String nick_name = obj_vars.optString("nick_name");//下单用户姓名
		String phone = obj_vars.optString("phone");//手机号
		String pwd = obj_vars.optString("pwd");//密码
		String is_forbid = obj_vars.optString("is_forbid");//	是否禁用：0正常；1禁用
		String brand_type = obj_vars.optString("brand_type");//品牌id
		String liststr = obj_vars.optString("list");//["1","2"...] 所管客户的id
		
		Map<String, String> map = new HashMap<>();
		map.put("nick_name", nick_name);
		map.put("phone", phone);
		map.put("pwd", basictool.MD5(pwd));
		map.put("is_forbid", is_forbid);
		map.put("brand_type", brand_type);
		
		int uid = sqlMapper.insert_or_update("p_user", map, null);
		
		//更新客户表
		Gson gson = new Gson();
		List<String> list = gson.fromJson(liststr, new TypeToken<List<String>>() {}.getType());
		for (String str : list) {
			String sqlupdatecus = "update p_customer set uid = %s where cid = %s";
			sqlMapper.update(String.format(sqlupdatecus, uid, Integer.valueOf(str)));
		}
		
		results.setMessage("添加成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:下单用户管理==>查看
	//{"mid":"1","action":"b_user_see_get","uid":"5"}
	public void b_user_see_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int uid = obj_vars.optInt("uid");//下单用户id
		
		String sql = "select pu.uid, IFNULL(pu.nick_name,insert(pu.phone, 4, 4, '****')) nick_name, pu.phone"
				+ " ,date_format(pu.create_time, '%Y-%m-%d') create_time, if(pu.is_forbid =0,'未禁用','已禁用') is_forbid "
				+ " ,pb.brand_name , group_concat(pc.company separator ',')  company_name, "
				+ " (case pu.approve_status when 1 then '未审核' when 2  then '已审核' when 3 then '已驳回' end ) approve_status "
				+ ", date_format(ha.create_time, '%Y-%m-%d %H:%i') approve_time, ha.approve_opinion "
				+ " from p_user pu "
				+ " left join p_brand pb on pb.brand_type = pu.brand_type "
				+ " left join p_customer pc on pc.uid = pu.uid  "
				+ " left join hx_approve ha on ha.ref_id = pu.uid where pu.uid = "+uid+"  ";
		Map<String, Object> map = sqlMapper.public_one_detail(sql);
		
		results.setResult(map);//输出：下单用户信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:下单用户管理==>编辑,审核查询
	//{"mid":"1","action":"b_user_edit_select_get","uid":"6"}
	public void b_user_edit_select_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int uid = obj_vars.optInt("uid");//下单用户id
		
		String sql = "select pu.uid, IFNULL(pu.nick_name,insert(pu.phone, 4, 4, '****')) nick_name, pu.phone, "
				+ " date_format(pu.create_time, '%Y-%m-%d') create_time, pu.is_forbid, pu.brand_type "
				+ " from p_user pu where pu.uid = "+uid+"";
		System.out.println(sql);
		Map<String, Object> map = sqlMapper.public_one_detail(sql);
		
		//获取用户所能下单的客户
		String sqlnull = "select cid, company, if(uid is null,2,1) is_have from p_customer where uid is null or uid = %s";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sqlnull, uid));
		
		map.put("list", list);
		//获取审核意见啊
		String sqlapprove = "select IFNULL(approve_opinion,''), IFNULL(DATE_FORMAT(create_time,'%Y-%m-%d %H:%i'),'') approve_time from hx_approve "
				+ " where approve_type = 100 and ref_id = "+uid+" and is_last = 1";
		Map<String, Object> map2 = sqlMapper.public_one_detail(sqlapprove);
		if (map2 == null) {
			map.put("approve_opinion", "");
			map.put("approve_time", "");
		} else {
			map.put("approve_opinion", map2.get("approve_opinion"));
			map.put("approve_time", map2.get("approve_time"));
		}
		
		results.setResult(map);//输出：下单用户信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:下单用户管理==>编辑
    //{"mid":"1","action":"b_user_edit_get","uid":"6"}
	public void b_user_edit_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int uid = obj_vars.optInt("uid");//下单用户id
		String nick_name = obj_vars.optString("nick_name");//下单用户姓名
		String phone = obj_vars.optString("phone");//电话
		String pwd = obj_vars.optString("pwd");//登录密码
		int is_forbid = obj_vars.optInt("is_forbid");//是否禁用：0正常；1禁用
		int brand_type = obj_vars.optInt("brand_type");//品牌
		String liststr = obj_vars.optString("list");//下属客户id["1","2","3"...]
		
		String sqlupdateuser = "update p_user set nick_name = '%s', phone = '%s', pwd = '%s', is_forbid = %s, brand_type = %s where uid = %s";
		sqlMapper.update(String.format(sqlupdateuser, nick_name, phone,basictool.MD5(pwd),is_forbid,brand_type,uid));
		
		//修改下属客户 在添加
		String sqldelete = "update p_customer set uid = null  where uid = %s";
		sqlMapper.update(String.format(sqldelete, uid));
		
		//新增下属客户
		Gson gson = new Gson();
		List<String> list = gson.fromJson(liststr, new TypeToken<List<String>>() {}.getType());
		for (String str : list) {
			String sqladd = "update p_customer set uid = %s  where cid = %s";
			sqlMapper.update(String.format(sqladd, uid, Integer.valueOf(str)));
		}
		
		results.setMessage("编辑成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:下单用户管理==>审核
    //{"mid":"1","action":"b_user_examine_get","uid":"6"}
	public void b_user_examine_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int uid = obj_vars.optInt("uid");//下单用户id
		int brand_type = obj_vars.optInt("brand_type");//品牌
		String liststr = obj_vars.optString("list");//下属客户id["1","2","3"...]
		int approve_status = obj_vars.optInt("approve_status");//审核状态 2已审核；3已驳回
		String approve_opinion = obj_vars.optString("approve_opinion");//审核意见
		
		int i = 1;
		
		List<String> list_customer = new ArrayList<>();
		
		String message = "审核成功";
		int status = 1;
		
		//判断下单客户是否都有自动下单设置
		Gson gson = new Gson();
		List<String> list_is = gson.fromJson(liststr, new TypeToken<List<String>>() {}.getType());
		for (String str : list_is) {
			int cid = Integer.valueOf(str);
			String sql_supply = "select count(1) from p_customer_supply where cid = %s";
			int a = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_supply, cid)));
			if (a < 0) {
				i = 2;
				String sql_customer = "select company from p_customer where cid = %s";
				String company = sqlMapper.public_one_scale(String.format(sql_customer, cid));
				list_customer.add(company);
			}
		}
		
		if (i == 1) { //都有自动下单设置
			String sqlupdateuser = "update p_user set approve_status = %s, brand_type = %s where uid = %s";
			sqlMapper.update(String.format(sqlupdateuser, approve_status,brand_type,uid));
			
			//修改下属客户 在添加
			String sqldelete = "update p_customer set uid = null  where uid = %s";
			sqlMapper.update(String.format(sqldelete, uid));
			
			//新增下属客户
			List<String> list = gson.fromJson(liststr, new TypeToken<List<String>>() {}.getType());
			for (String str : list) {
				String sql_add = "update p_customer set uid = %s  where cid = %s";
				sqlMapper.update(String.format(sql_add, uid, Integer.valueOf(str)));
			}
			
			//更新之前的审核
			String sqlupdate = "update hx_approve set is_last = 0 where ref_id = %s";
			sqlMapper.update(String.format(sqlupdate, uid));
			//新增审核
			String sqladd = "insert into hx_approve (approve_status, approve_opinion, "
					+ "member_id, approve_type, ref_id, is_last) values (%s,'%s',%s,100,%s,1)";
			sqlMapper.insert(String.format(sqladd, approve_status, approve_opinion, member_id, uid));
		} else {
			message = "未能审核";
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("is_supply", i);
		map.put("list", list_customer);
		
		results.setResult(map); //is_supply 1:可以提交 2:不能提交   list:不能提交的客户名称
		results.setMessage(message);//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:用户反馈==>列表
	//{"mid":"1","action":"b_feedback_list_get","pagenum":"0","pagesize":"0"}
	public void b_feedback_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String nick_name = obj_vars.optString("nick_name");//用户名
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT * FROM  (select pf.feedback_id, IFNULL(pus.nick_name,insert(phone, 4, 4, '****')) nick_name, "
				+ " (case pus.utype when 1 then '客户' when 2 then '供应商' end) utype, "
				+ "  pf.content, DATE_FORMAT(pf.create_time,'%Y-%m-%d %H:%i') create_time, "
				+ " (case pf.status when 1 then '未处理' when 2 then '已处理' end) status,  "
				+ " DATE_FORMAT(pf.update_time,'%Y-%m-%d %H:%i') update_time, pf.update_content from p_feedback pf "
				+ " inner join p_user pus on pus.uid = pf.uid order by pf.create_time desc) b "
				+ " WHERE 1=1 ";
		
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(b.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(b.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(nick_name)) {
			sql+= " and b.nick_name like '%"+nick_name+"%' ";
		}
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		results.setResult(pageModel);//输出：问题反馈列表
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:用户反馈==>详情
	//{"mid":"1","action":"b_feedback_detail_get","feedback_id":"1"}
	public void b_feedback_detail_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int feedback_id = obj_vars.optInt("feedback_id");//反馈id
		
		String sql = "select * from (select pf.feedback_id, IFNULL(pus.nick_name,insert(phone, 4, 4, '****')) nick_name, "
				+ " (case pus.utype when 1 then '客户' when 2 then '供应商' end) utype, "
				+ "  pf.content, DATE_FORMAT(pf.create_time,'%Y-%m-%d %H:%i') create_time, "
				+ " (case pf.status when 1 then '未处理' when 2 then '已处理' end) status,  "
				+ " DATE_FORMAT(pf.update_time,'%Y-%m-%d %H:%i') update_time, pf.update_content from p_feedback pf "
				+ " inner join p_user pus on pus.uid = pf.uid order by pf.create_time desc) b "
				+ " where  b.feedback_id = "+feedback_id+"";
		Map<String, Object> map = sqlMapper.public_one_detail(sql);
		
		//反馈图片 list
		String sql_list = "select content from p_content where business_type = 100 and business_id = %s and content_type = 2 order by ordid ";
		List<Map<String, Object>> list_pic = sqlMapper.public_list_all_sql(String.format(sql_list, feedback_id));
		
		List<String> list = new ArrayList<>();
		
		for (Map<String, Object> map_pic : list_pic) {
			String content = map_pic.get("content").toString();
			if (content.indexOf("http_filepath = ") != -1) {
				list.add(StringUtils.substringAfter(content, "http_filepath = "));
			}
		}
		map.put("list", list);
		
		results.setResult(map);//输出：问题反馈数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:用户反馈==>处理
	//{"mid":"1","action":"b_feedback_update_set"}
	public void b_feedback_update_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int feedback_id = obj_vars.optInt("feedback_id");//反馈id
		String update_content = obj_vars.optString("update_content");//处理结果
		
		String sql = "update p_feedback set update_time = now(), status = 2, update_content = '%s' where feedback_id = %s";
		sqlMapper.update(String.format(sql, update_content,feedback_id));
		
		results.setMessage("处理成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//gengek:新增表 p_adv adv_name
	//接口功能:轮播图管理==>列表
	//{"mid":"1","action":"b_adv_list_get","pagenum":"0","pagesize":"0"}
	public void b_adv_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT pa.adv_id, pa.adv_name, pa.adv_type, pb.brand_name"
				+ "   FROM p_adv pa "
				+ "inner join p_brand pb on pa.brand_type = pb.brand_type "
				+ "WHERE adv_position = 1  order by pa.create_time desc ";
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		results.setResult(pageModel);//输出：轮播图管理列表
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:轮播图管理==>添加
	public void b_adv_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String adv_name = obj_vars.optString("adv_name");//轮播图名称
		String adv_img = obj_vars.optString("adv_img");//轮播图图片地址
		int adv_type = obj_vars.optInt("adv_type");//轮播图类型 1仅图片(不支持点击)；2链接；3富文本html
		String adv_content = obj_vars.optString("adv_content");//与adv_type对应：1空；2链接字符串；3富文本html
		int brand_type = obj_vars.optInt("brand_type");//品牌 1.健康快车 2.天之源
		
		String sql = "insert into p_adv (adv_name,adv_img,adv_type,adv_content,brand_type,adv_position) values ('%s','%s',%s,'%s',%s,1)";
		sqlMapper.insert(String.format(sql, adv_name,adv_img,adv_type,adv_content,brand_type));
		
		results.setMessage("保存成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:轮播图管理==>修改查询
	public void b_adv_edit_select_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int adv_id = obj_vars.optInt("adv_id");//轮播图id
		
		String sql = "select adv_id,  adv_img, f_img(adv_img,0) http_adv_img, adv_content, adv_name, adv_type, brand_type from p_adv where adv_id = %s";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql, adv_id));
		
		results.setResult(map);//输出:轮播图信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:轮播图管理==>修改
	public void b_adv_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int adv_id = obj_vars.optInt("adv_id");//轮播图id
		
		String adv_name = obj_vars.optString("adv_name");//轮播图名称
		String adv_img = obj_vars.optString("adv_img");//轮播图图片地址
		int adv_type = obj_vars.optInt("adv_type");//轮播图类型 1仅图片(不支持点击)；2链接；3富文本html
		String adv_content = obj_vars.optString("adv_content");//与adv_type对应：1空；2链接字符串；3富文本html
		int brand_type = obj_vars.optInt("brand_type");//品牌 1.健康快车 2.天之源
		
		String sql = "update p_adv set adv_name = '%s', adv_img = '%s', adv_type = %s, adv_content = '%s', brand_type = %s where adv_id = %s";
		sqlMapper.update(String.format(sql, adv_name,adv_img,adv_type,adv_content,brand_type,adv_id));
		
		results.setMessage("修改成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:首页底部图片管理(修改使用 b_adv_edit_set)
	public void b_adv_bottom_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int brand_type  = obj_vars.optInt("brand_type");//品牌类型
		
		String sql = "select adv_id,  adv_img, f_img(adv_img,0) http_adv_img, adv_content,"
				+ " adv_name, adv_type, brand_type from p_adv where "
				+ " adv_position = 2 and brand_type = "+brand_type+" ";
		Map<String, Object> map = sqlMapper.public_one_detail(sql);
		
		results.setResult(map);//输出:首页底部图片管理信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:首页栏目管理==>列表
	public void b_catalog_home_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT pch.home_catalog_id, pc.catalog_name, pch.home_catalog_img,  f_img(pch.home_catalog_img,0) http_home_catalog_img , pch.ordid "
				+ " FROM p_catalog_home pch inner join p_catalog pc on pch.catalog_id = pc.catalog_id order by pch.create_time desc ";
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		results.setResult(pageModel);//输出:首页栏目信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:首页栏目管理==>添加
	public void b_catalog_home_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int catalog_id = obj_vars.optInt("catalog_id");//大类id
		String home_catalog_img = obj_vars.optString("home_catalog_img");//图片地址
		int ordid = obj_vars.optInt("ordid");//排序号
		
		String message = "";
		//判断是否满10个
		String sqlnum = "select count(1) from p_catalog_home";
		int num =  Integer.valueOf(sqlMapper.public_one_scale(sqlnum));
		
		if (!(num >= 10)) { //数量
			String sql = "insert into p_catalog_home (catalog_id,home_catalog_img,ordid) values (%s,'%s',%s)";
			sqlMapper.insert(String.format(sql, catalog_id,home_catalog_img,ordid));
			message = "保存成功";
		} else {
			message = "数量超额";
		}
		
		results.setMessage(message);//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:首页栏目管理==>编辑查询
	public void b_catalog_home_edit_select_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int home_catalog_id = obj_vars.optInt("home_catalog_id");//首页栏目id
		
		String sql = "select home_catalog_id, catalog_id, home_catalog_img, f_img(home_catalog_img,0) http_home_catalog_img, ordid from p_catalog_home "
				+ " where home_catalog_id = %s ";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql, home_catalog_id));
		
		results.setResult(map);//输出:首页栏目信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	

	//接口功能:首页栏目管理==>编辑
	public void b_catalog_home_edit_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int home_catalog_id = obj_vars.optInt("home_catalog_id");//首页栏目id
		int catalog_id = obj_vars.optInt("catalog_id");//大类id
		String home_catalog_img = obj_vars.optString("home_catalog_img");//图片地址
		int ordid = obj_vars.optInt("ordid");//排序号
		
		String sql = "update p_catalog_home set catalog_id = %s, home_catalog_img = '%s', ordid = %s "
				+ " where home_catalog_id = %s ";
		sqlMapper.update(String.format(sql, catalog_id,home_catalog_img,ordid,home_catalog_id));
		
		results.setMessage("编辑成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能: 账单格式
	//{"mid":"1","action":"b_bill_format_type_list_get","company":"","pagenum":"0","pagesize":"0"}
	public void b_bill_format_type_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(可空)
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = " SELECT pc.cid, pc.company, pc.contact_phone, date_format(pc.create_time,'%Y-%m-%d') create_time ,  "
				+ " (case  when pu.is_forbid is null then '未指定用户' when pu.is_forbid = 0 then '未禁用' when pu.is_forbid = 1 then '已禁用' end) is_forbid  "
				+ "FROM p_customer pc left join p_user pu on pu.uid = pc.uid "
				+ " WHERE 1=1 ";
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and pc.company like '%"+kehu_company+"%' ";
		}
		sql+= " order by pc.create_time desc ";
		
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		results.setResult(pageModel);//输出:账单格式列表
		results.setMessage("编辑成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:账单格式详情
	//{"mid":"1","action":"b_bill_format_type_detail_get","customer_id":"1"}
	public void b_bill_format_type_detail_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int customer_id = obj_vars.optInt("customer_id");//客户id
		
		//客户的制单格式
		String sql = "select if(bill_format_type is null,'0', bill_format_type) bill_format_type from p_customer where cid = %s";
		String bill_format_type = sqlMapper.public_one_scale(String.format(sql, customer_id));
		
		//账单格式列表
		String sql_bill = "select bill_format_type, bill_format_name, template_src  from hx_bill_format  ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql_bill);
		
		Map<String, Object> map = new HashMap<>();
		map.put("bill_format_type", bill_format_type);
		map.put("list", list);
		
		results.setResult(map);//输出:账单格式详情
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:账单格式更改
	//{"mid":"1","action":"b_bill_format_type_update_get","customer_id":"2","bill_format_type":"2"}
	public void b_bill_format_type_update_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int customer_id = obj_vars.optInt("customer_id");//客户id
		int bill_format_type = obj_vars.optInt("bill_format_type");//账单格式id
		
		String sql = "update p_customer set bill_format_type = %s where cid = %s";
		sqlMapper.update(String.format(sql, bill_format_type, customer_id));
		
		results.setMessage("修改成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:订单提醒==>查询
	public void b_order_remind_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		
		String sql = "select set_value from p_setting where set_type = '%s'";
		int is_remind = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql, SetTypeEnum.order_remind.getId())));
		
		results.setResult(is_remind);//输出:订单提醒  is_remind 1是 2不是
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:订单提醒==>开启or关闭
	public void b_order_remind_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int is_remind = obj_vars.optInt("is_remind");//is_remind 1是 2不是
		
		String sql = "update p_setting set set_value = %s where set_type = '%s'";
		sqlMapper.update(String.format(sql, is_remind,SetTypeEnum.order_remind.getId()));
		
		results.setMessage("修改成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:消息管理==>系统通知
	//{"mid":"1","action":"b_message_system_get","pagenum":"0","pagesize":"0"}
	public void b_message_system_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT pmd.msg_define_id, pmd.msg_title, date_format(pmd.create_time,'%Y-%m-%d') create_time, "
				+ " (select pb.brand_name from p_brand pb where pmd.brand_id = pb.brand_type) brand_name,"
				+ " if(pmd.msg_type = 200,'用户端','商家端') end, pmd.send_num  "
				+ " FROM p_message_define pmd  "
				+ " WHERE pmd.msg_type = 200 or pmd.msg_type = 500 ";
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		results.setResult(pageModel);//输出:系统通知  send_num 推送次数  0表示未推送
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:消息管理==>系统通知-添加
	public void b_message_system_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String msg_title = obj_vars.optString("msg_title");//系统通知标题
		String msg_content = obj_vars.optString("msg_content");//系统通知详细内容
		int msg_type = obj_vars.optInt("msg_type");//接收方 200：用户端，500：商家端
		int is_push = obj_vars.optInt("is_push");//是否推送 1:是2:不
		int brand_id = obj_vars.optInt("brand_id");//brand_id  品牌
		
		Map<String, String> map = new HashMap<>();
		map.put("msg_title", msg_title);
		map.put("msg_content", msg_content);
		map.put("brand_id", String.valueOf(brand_id));
		map.put("msg_type", String.valueOf(msg_type));
		
		int msg_define_id = sqlMapper.insert_or_update("p_message_define", map, null);
		
		int utype = 0;
		if(msg_type == 200) {
			utype = 1;
		}
		if(msg_type == 400) {
			utype = 2;
		}
		
		if(is_push == 1) {
			send_message(msg_type, msg_content, utype, brand_id, msg_title);
			//增加推送次数
			String sql_send_num = "update p_message_define set send_num = send_num+1 where msg_define_id = %s";
			sqlMapper.update(String.format(sql_send_num, msg_define_id));
		}
		
		results.setMessage("操作成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:消息管理==>系统通知-编辑查询
	//{"mid":"1","action":"b_message_system_edit_select_set","msg_define_id":"1"}
	public void b_message_system_edit_select_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int msg_define_id = obj_vars.optInt("msg_define_id");//系统通知id
		int is_push = obj_vars.optInt("is_push");//是否推送 1:是2:不
		
		String sql = "select pmd.msg_define_id, pmd.msg_content, pmd.msg_title, "
				+ "DATE_FORMAT(pmd.create_time,'%Y-%m-%m') create_time, pmd.brand_id,"
				+ " pmd.msg_type, pmd.send_num  from p_message_define pmd  "
				+ " inner join p_brand pb on pb.brand_type = pmd.brand_id where pmd.msg_define_id = "+msg_define_id+" ";
		Map<String, Object> map = sqlMapper.public_one_detail(sql);
		
		results.setResult(map);//输出:系统通知详情
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:消息管理==>系统通知-编辑
	//{"mid":"1","action":"b_message_system_edit_set","msg_define_id":"7","msg_title":"lala111","msg_content":"您的订单已发货完成11","create_time":"2018-09-22 10:03:42","msg_type":"400","is_push":"1","brand_id":"1"}
	public void b_message_system_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int msg_define_id = obj_vars.optInt("msg_define_id");//系统通知id
		String msg_title = obj_vars.optString("msg_title");//系统通知标题
		String msg_content = obj_vars.optString("msg_content");//系统通知详细内容
		int msg_type = obj_vars.optInt("msg_type");//接收方 200：用户端，500：商家端
		int is_push = obj_vars.optInt("is_push");//是否推送 1:是2:不
		int brand_id = obj_vars.optInt("brand_id");//brand_id 品牌id
		
		String sql = "update p_message_define set msg_title = '%s', msg_content = '%s',  msg_type = %s , brand_id = %s  where msg_define_id = %s";
		System.out.println(String.format(sql, msg_title,msg_content,msg_type,brand_id,msg_define_id));
		sqlMapper.update(String.format(sql, msg_title,msg_content,msg_type,brand_id,msg_define_id));
		
		int utype = 0;
		if(msg_type == 200) {
			utype = 1;
		}
		if(msg_type == 400) {
			utype = 2;
		}
		
		if(is_push == 1) {
			send_message(msg_type, msg_content, utype,brand_id, msg_title);
			//增加推送次数
			String sql_send_num = "update p_message_define set send_num = send_num+1 where msg_define_id = %s";
			sqlMapper.update(String.format(sql_send_num, msg_define_id));
		}
		
		results.setMessage("操作成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:消息管理==>公告信息列表
	//{"mid":"1","action":"b_message_notice_add_get","pagenum":"0","pagesize":"0"}
	public void b_message_notice_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT pmd.msg_define_id, pmd.msg_title, date_format(pmd.create_time,'%Y-%m-%d') create_time, "
				+ " (select pb.brand_name from p_brand pb where pmd.brand_id = pb.brand_type) brand_name, pmd.send_num"
				+ " FROM p_message_define pmd  "
				+ " WHERE pmd.msg_type = 300 or pmd.msg_type = 301 ";
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		results.setResult(pageModel);//输出:系统通知  send_num 推送次数  0表示未推送
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:消息管理==>公告信息==》添加
	//{"mid":"1","action":"b_message_notice_get","pagenum":"0","pagesize":"0"}
	public void b_message_notice_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String msg_title = obj_vars.optString("msg_title");//公告信息标题
		String msg_content = obj_vars.optString("msg_content");//公告信息详细内容
		int is_push = obj_vars.optInt("is_push");//是否推送 1:是2:不
		int brand_id = obj_vars.optInt("brand_id");//brand_id 品牌id
		
		Map<String, String> map = new HashMap<>();
		map.put("msg_title", msg_title);
		map.put("msg_content", msg_content);
		map.put("brand_id", String.valueOf(brand_id));
		map.put("msg_type", "301");
		
		int msg_define_id = sqlMapper.insert_or_update("p_message_define", map, null);
		
		if(is_push == 1) {
			send_message(MsgTypeEnum.client_notice_notice.getId(), msg_content, 1,brand_id, msg_title);
			//增加推送次数
			String sql_send_num = "update p_message_define set send_num = send_num+1 where msg_define_id = %s";
			sqlMapper.update(String.format(sql_send_num, msg_define_id));
		}
		
		results.setMessage("操作成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:消息管理==>公告信息-编辑查询
	//{"mid":"1","action":"b_message_notice_edit_select_set","msg_define_id":"1"}
	public void b_message_notice_edit_select_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int msg_define_id = obj_vars.optInt("msg_define_id");//公告信息id
		int is_push = obj_vars.optInt("is_push");//是否推送 1:是2:不
		
		String sql = "select pmd.msg_define_id, pmd.msg_content, DATE_FORMAT(pmd.create_time,'%Y-%m-%m') create_time, pmd.brand_id, pmd.send_num  from p_message_define pmd  "
				+ " inner join p_brand pb on pb.brand_type = pmd.brand_id where pmd.msg_define_id = "+msg_define_id+" ";
		Map<String, Object> map = sqlMapper.public_one_detail(sql);
		
		results.setResult(map);//输出:公告信息详情
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:消息管理==>公告信息-编辑
	//{"mid":"1","action":"b_message_notice_edit_set","msg_define_id":"7","msg_title":"lala111","msg_content":"您的订单已发货完成11","create_time":"2018-09-22 10:03:42","msg_type":"400","is_push":"1","brand_id":"1"}
	public void b_message_notice_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int msg_define_id = obj_vars.optInt("msg_define_id");//公告信息id
		String msg_title = obj_vars.optString("msg_title");//公告信息标题
		String msg_content = obj_vars.optString("msg_content");//公告信息详细内容
		int is_push = obj_vars.optInt("is_push");//是否推送 1:是2:不
		int brand_id = obj_vars.optInt("brand_id");//brand_id 品牌id
		
		String sql = "update p_message_define set msg_title = '%s', msg_content = '%s' , brand_id = %s  where msg_define_id = %s";
		sqlMapper.update(String.format(sql, msg_title,msg_content,brand_id,msg_define_id));
		
		if(is_push == 1) {
			send_message(MsgTypeEnum.client_notice_notice.getId(), msg_content, 1,brand_id, msg_title);
			//增加推送次数
			String sql_send_num = "update p_message_define set send_num = send_num+1 where msg_define_id = %s";
			sqlMapper.update(String.format(sql_send_num, msg_define_id));
		}
		
		results.setMessage("操作成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:栏目管理==>列表(暂不使用)
	//{"mid":"1","action":"b_catalog_list_get"}
	public void b_catalog_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		
		String sql = "select pcp.catalog_id catalog_parent_id, pcp.catalog_name catalog_parent_name, pcp.is_msg is_msg_parent, pc.catalog_id, pc.catalog_name, pc.is_msg from p_catalog pc "
				+ " inner join p_catalog pcp on pcp.catalog_id = pc.pid where pc.pid != 0 order by pcp.ordid is null, pcp.catalog_id, pc.ordid is null, pc.catalog_id ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = null;
		List<Map<String,Object>> lstMapitems = null;
		String catalog_parent_id = "";
		for(Map<String,Object> mapItem: list)
		{
			if(catalog_parent_id.isEmpty() || !catalog_parent_id.equals(mapItem.get("catalog_parent_id").toString()))
			{
				map = new HashMap<String,Object>();
				catalog_parent_id = mapItem.get("catalog_parent_id").toString();
				map.put("catalog_parent_id", mapItem.get("catalog_parent_id"));
				map.put("catalog_parent_name", mapItem.get("catalog_parent_name"));
				map.put("is_msg_parent", mapItem.get("is_msg_parent"));
				
				listMap.add(map);
				//子类
				lstMapitems = new ArrayList<Map<String,Object>>();
				map.put("items", lstMapitems);
			}
			
			Map<String,Object> mapCatalogItem = new HashMap<String,Object>(); 
			mapCatalogItem.put("catalog_id", mapItem.get("catalog_id"));
			mapCatalogItem.put("catalog_name", mapItem.get("catalog_name"));
			mapCatalogItem.put("is_msg", mapItem.get("is_msg"));
			lstMapitems.add(mapCatalogItem);
		}
		
		results.setResult(listMap);//输出:分类信息 is_msg is_msg_parent 0:提箱 1:不提醒
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:栏目管理==>获取大类
	//{"mid":"1","action":"b_catalog_parent_list_get"}
	public void b_catalog_parent_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		
		String sql = "select catalog_id catalog_parent_id, catalog_name catalog_parent_name, is_msg from p_catalog where pid = 0 order by ordid is null, catalog_id";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		results.setResult(list);//输出:大类信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:栏目管理==>获取子类
	//{"mid":"1","action":"b_catalog_subclass_list_get","catalog_parent_id":"1"}
	public void b_catalog_subclass_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int catalog_parent_id = obj_vars.optInt("catalog_parent_id");//父类id
		
		String sql = "select catalog_id, catalog_name, is_msg from p_catalog where pid = "+catalog_parent_id+" order by ordid is null, catalog_id ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		results.setResult(list);//输出:子类信息信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:栏目管理==>大类编辑查询
	//{"mid":"1","action":"b_catalog_parent_edit_select_get"}
	public void b_catalog_parent_edit_select_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int catalog_parent_id = obj_vars.optInt("catalog_parent_id");//父类id
		
		String sql = "select catalog_id catalog_parent_id, catalog_name catalog_parent_name, is_msg, ordid from p_catalog where catalog_id = %s";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql, catalog_parent_id));
		
		results.setResult(map);//输出:子类信息信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:栏目管理==>大类编辑
	//{"mid":"1","action":"b_catalog_parent_edit_get","catalog_parent_id":"1","catalog_parent_name":"1","ordid":"1","is_msg":"1"}
	public void b_catalog_parent_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int catalog_parent_id = obj_vars.optInt("catalog_parent_id");//父类id
		String catalog_parent_name = obj_vars.optString("catalog_parent_name");//父类名称
		int ordid = obj_vars.optInt("ordid");//排列序号
		int is_msg = obj_vars.optInt("is_msg");//是否有订单消息 is_msg 0有1没有
		
		String sql = "update p_catalog set catalog_name = '%s', ordid = %s , is_msg = %s where catalog_id = %s";
		sqlMapper.update(String.format(sql, catalog_parent_name,ordid,is_msg,catalog_parent_id));
		
		results.setMessage("编辑成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:栏目管理==>子类编辑查询
	//{"mid":"1","action":"b_catalog_subclass_edit_select_get"}
	public void b_catalog_subclass_edit_select_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int catalog_id = obj_vars.optInt("catalog_id");//子类id
		
		String sql = "select pid catalog_parent_id, catalog_id, catalog_name, is_msg, "
				+ "ordid, DATE_FORMAT(deadline,'%H:%i') deadline from p_catalog where catalog_id = "+catalog_id+"";
		Map<String, Object> map = sqlMapper.public_one_detail(sql);
		
		results.setResult(map);//输出:子类信息信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:栏目管理==>子类编辑
	//{"mid":"1","action":"b_catalog_subclass_edit_set","catalog_parent_id":"1","catalog_id":"1","catalog_name":"1","ordid":"1","is_msg":"1","deadline":"15:00"}
	public void b_catalog_subclass_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int catalog_parent_id = obj_vars.optInt("catalog_parent_id");//父类id
		int catalog_id = obj_vars.optInt("catalog_id");//子类id
		String catalog_name = obj_vars.optString("catalog_name");//子类名称
		int ordid = obj_vars.optInt("ordid");//排列序号
		int is_msg = obj_vars.optInt("is_msg");//是否有订单消息 is_msg 0有1没有
		String deadline = obj_vars.optString("deadline");//截止时间点
		
		String sql = "update p_catalog set pid = %s, catalog_name = '%s', ordid = %s , is_msg = %s, deadline = '%s' where catalog_id = %s";
		sqlMapper.update(String.format(sql,catalog_parent_id,catalog_name,ordid,is_msg,deadline,catalog_id));
		
		results.setMessage("编辑成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:栏目管理==>添加大类
	public void b_catalog_parent_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String catalog_parent_name = obj_vars.optString("catalog_parent_name");//父类名称
		int ordid = obj_vars.optInt("ordid");//排列序号
		int is_msg = obj_vars.optInt("is_msg");//是否有订单消息 is_msg 0有1没有
		
		String sql = "insert into p_catalog (catalog_name,is_msg,ordid) values ('%s',%s,%s)";
		sqlMapper.insert(String.format(sql, catalog_parent_name,is_msg,ordid));
		
		results.setMessage("添加成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:栏目管理==>添加子类
	public void b_catalog_subclass_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int catalog_parent_id = obj_vars.optInt("catalog_parent_id");//父类id
		String catalog_name = obj_vars.optString("catalog_name");//子类名称
		int ordid = obj_vars.optInt("ordid");//排列序号
		int is_msg = obj_vars.optInt("is_msg");//是否有订单消息 is_msg 0有1没有
		String deadline = obj_vars.optString("deadline");//截止时间点
		
		String sql = "insert into p_catalog (pid,catalog_name,is_msg,ordid,deadline) values (%s,'%s',%s,%s,'%s')";
		sqlMapper.insert(String.format(sql, catalog_parent_id,catalog_name,is_msg,ordid,deadline));
		
		results.setMessage("添加成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:商品管理==>列表
	//{"mid":"1","action":"b_item_list_get","catalog_parent_id":"1","pagenum":"0","pagesize":"0"}
	public void b_item_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String item_name = obj_vars.optString("item_name");//商品名称
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//子类id
		String status = obj_vars.optString("status");
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT pi.item_id, (select pcp.catalog_name from p_catalog pcp where pcp.catalog_id = pc.pid) catalog_name_parent, "
				+ " 	pc.catalog_name,pi.item_name,pi.item_id,pi.ordid,DATE_FORMAT(pi.deadline,'%H:%i') deadline,pi.status,pi.unit "
				+ " FROM p_item pi "
				+ " left join p_catalog pc on pc.catalog_id = pi.catalog_id  "
				+ " WHERE 1=1 and pi.is_delete = 2 ";
				
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and pi.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id in (select catalog_id from p_catalog where pid = "+catalog_parent_id+")  ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id =  "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(status)) {
			sql+= " and pi.status= "+status+" ";
		}
		sql+=" ORDER BY pc.catalog_id, pi.item_id  ";
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		results.setResult(pageModel);//输出:商品管理
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:商品管理==>上架下架
	//{"mid":"1","action":"b_item_up_set","item_id":"0","status":"0"}
	public void b_item_up_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int item_id = obj_vars.optInt("item_id");//商品id
		int status = obj_vars.optInt("status");//商品状态	状态：1上架；2下架
		
		String sql = "update p_item set status = %s where item_id = %s ";
		sqlMapper.update(String.format(sql, status, item_id));
		
		results.setMessage("修改成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:商品管理==》添加==》获取属性列表
	//{"mid":"1","action":"b_item_attr_list_get","attr_name":"","attr_ids":"(1,2)"}
	public void b_item_attr_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String attr_name = obj_vars.optString("attr_name");//属性名称
		String attr_ids = obj_vars.optString("attr_ids");//已选的属性id (1,2) 拼成字符串
		
		String sql = "select attr_id, attr_name from p_attr where 1=1  ";
		if (!StringUtil.isEmpty(attr_name)) {
			sql+= " and attr_name like '%"+attr_name+"%' ";
		}
		if (!StringUtil.isEmpty(attr_ids)) {
			sql+= " and attr_id not in "+attr_ids+" ";
		}
		
		List<Map<String,Object>> list = sqlMapper.public_list_all_sql(sql);
		
		results.setResult(list);//输出:列表数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:商品管理==》添加==》获取属性值
	//{"mid":"1","action":"b_item_attr_value_get","attr_ids":"(1,2)"}
	public void b_item_attr_value_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String attr_ids = obj_vars.optString("attr_ids");//attr_ids (1,2,3...)
		
		String sql = "select pav.attr_id, (select pa.attr_name from p_attr pa where pa.attr_id = pav.attr_id ) attr_name, "
				+ "pav.val_id, pav.val_name from p_attr_val pav where pav.attr_id in %s order by pav.val_id";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sql, attr_ids));
		
		List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();
		Map<String,Object> map1 = null;
		List<Map<String,Object>> lstMapitems = null;
		String attr_id = "";
		for(Map<String,Object> mapItem: list)
		{
			if(attr_id.isEmpty() || !attr_id.equals(mapItem.get("attr_id").toString()))
			{
				map1 = new HashMap<String,Object>();
				attr_id = mapItem.get("attr_id").toString();
				map1.put("attr_id", mapItem.get("attr_id"));
				map1.put("attr_name", mapItem.get("attr_name"));
				listMap.add(map1);
				
				lstMapitems = new ArrayList<Map<String,Object>>();
				map1.put("items", lstMapitems);
			}
			
			Map<String,Object> mapItem1 = new HashMap<String,Object>(); 
			mapItem1.put("val_id", mapItem.get("val_id"));
			mapItem1.put("val_name", mapItem.get("val_name"));
			lstMapitems.add(mapItem1);
		}
		
		results.setResult(listMap);//输出:属性值数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:商品管理==》添加==》基础设置
	//{"mid":"1","action":"b_item_add_set","catalog_id":"63","item_name":"青龙蛋","ordid":"100","deadline":"15:20","status":"1","unit":"个","is_sku":"1","uniform_price":"","list":[{"val_ids":"4,5,11","val_names":"青菜,红,1L","uniform_price":"2000000"}]}
	public void b_item_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String catalog_id = obj_vars.optString("catalog_id");//小类id
		String item_name = obj_vars.optString("item_name");//商品名称
		String ordid = obj_vars.optString("ordid");//序号
		String deadline = obj_vars.optString("deadline");//截止时间点
		String status = obj_vars.optString("status");//是否上架 (1上架；2下架)
		String unit = obj_vars.optString("unit");//单位
		String item_small_img = obj_vars.optString("item_small_img");//分类下面的商品小图
		String item_img = obj_vars.optString("item_img"); //商品详情图片
		String item_desc = obj_vars.optString("item_desc");//商品描述
		String is_sku = obj_vars.optString("is_sku");//是否有多规格设置 (0:否;1:是) 0 uniform_price 有值  1 list 有值 
		String uniform_price = obj_vars.optString("uniform_price");//单规格价格
		String liststr = obj_vars.optString("list");//规格信息    list:[{"val_ids":"","val_names":"","uniform_price":""}]
		
		Map<String, String> map = new HashMap<>();
		map.put("catalog_id", catalog_id);
		map.put("item_name", item_name);
		map.put("ordid", ordid);
		map.put("deadline", deadline);
		map.put("status", status);
		map.put("unit", unit);
		map.put("is_sku", is_sku);
		map.put("item_small_img", item_small_img);
		map.put("item_img", item_img);
		map.put("item_desc", item_desc);
		if (Integer.valueOf(is_sku) == 0) { //单规格
			map.put("uniform_price", uniform_price);
			map.put("max_price", uniform_price);
		} else {
			List<Double> uniform_price_list = new ArrayList<>();
			Gson gson = new Gson();
			List<Map<String, Object>> list = gson.fromJson(liststr, new TypeToken<List<Map<String, Object>>>() {}.getType());
			for (Map<String, Object> map_uniform_price : list) {
				uniform_price_list.add(Double.valueOf(map_uniform_price.get("uniform_price").toString()));
				map.put("uniform_price", String.valueOf(Collections.min(uniform_price_list)));
				map.put("max_price", String.valueOf(Collections.max(uniform_price_list)));
			}
		}
		
		//商品表 p_item
		int item_id =  sqlMapper.insert_or_update("p_item", map, null);
		
		if (Integer.valueOf(is_sku) == 0) { //单规格
			//商品属性值表 p_item_attr
			String sql_item_attr = "insert into p_item_attr (item_id,attr_id,val_id) values (%s,1,1)";
			sqlMapper.insert(String.format(sql_item_attr, item_id));
			//商品规格表  p_sku
			Map<String, String> mapsku = new HashMap<>();
			mapsku.put("item_id", String.valueOf(item_id));
			mapsku.put("val_ids", "1");
			mapsku.put("val_names", "无");
			mapsku.put("uniform_price", uniform_price);
			int sku_id = sqlMapper.insert_or_update("p_sku", mapsku, null);
			//规格属性值表 p_sku_attr
			String sql_sku_attr = "insert into p_sku_attr (item_id,sku_id,attr_id,val_id) values (%s,%s,%s,%s)";
			sqlMapper.insert(String.format(sql_sku_attr, item_id,sku_id,1,1));
		} else {
			Gson gson = new Gson();
			List<Map<String, Object>> list = gson.fromJson(liststr, new TypeToken<List<Map<String, Object>>>() {}.getType());
			for (Map<String, Object> map2 : list) {
				String val_ids = map2.get("val_ids").toString();
				String val_names = map2.get("val_names").toString();
				String uniform_prices = map2.get("uniform_price").toString();
				String attr_title = "";
				String[] strs = val_ids.split(",");
				for (String str : strs) {
					//商品属性值表 p_item_attr
					String sql_item_attr = "insert into p_item_attr (item_id,attr_id,val_id) values (%s,(select attr_id from p_attr_val where val_id = %s),%s)";
					sqlMapper.insert(String.format(sql_item_attr, item_id,Integer.valueOf(str),Integer.valueOf(str)));
					//拼接attr_title
					String sql_attr_title = "select val_name from p_attr_val where val_id = %s";
					attr_title+= sqlMapper.public_one_scale(String.format(sql_attr_title, str));
				}
				//商品规格表  p_sku
				Map<String, String> mapsku = new HashMap<>();
				mapsku.put("item_id", String.valueOf(item_id));
				mapsku.put("attr_title", attr_title);
				mapsku.put("val_ids", val_ids);
				mapsku.put("val_names", val_names);
				mapsku.put("uniform_price", uniform_prices);
				int sku_id = sqlMapper.insert_or_update("p_sku", mapsku, null);
				for (String str : strs) {
					//规格属性值表 p_sku_attr
					String sql_sku_attr = "insert into p_sku_attr (item_id,sku_id,attr_id,val_id) values (%s,%s,(select attr_id from p_attr_val where val_id = %s),%s)";
					sqlMapper.insert(String.format(sql_sku_attr, item_id,sku_id,Integer.valueOf(str),Integer.valueOf(str)));
				}
			}
		}
		
		results.setMessage("添加成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	
	//接口功能:商品管理==》编辑==》编辑查询
	//{"mid":"1","action":"b_item_edit_get","item_id":"50"}
	public void b_item_edit_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int item_id = obj_vars.optInt("item_id");//商品id
		
		String sql = "select pi.item_id, (select pid from p_catalog where catalog_id = pi.catalog_id) catalog_parent_id, pi.catalog_id, "
				+ " pi.item_name, pi.ordid, DATE_FORMAT(pi.deadline,'%H:%i') deadline, pi.status, pi.unit, pi.is_sku, pi.uniform_price, item_img, f_img(item_img,0) http_item_img, "
				+ " item_small_img, f_img(item_small_img,0) http_item_small_img, item_desc "
				+ " from p_item pi where pi.item_id = "+item_id+" ";
		Map<String, Object> map = sqlMapper.public_one_detail(sql);
		List<Map<String,Object>> listMap = null;
		//判断是否有规格
		if (map.get("is_sku").toString().equals("1")) {//有规格
			String sql_sku = "select attr_title, val_ids, val_names, uniform_price from p_sku where item_id = %s and is_delete = 2 order by sku_id";
			listMap = sqlMapper.public_list_all_sql(String.format(sql_sku, item_id));
			
		}
		//规格展示数据
		map.put("skus", listMap);
		//规格原始数据
		String sql_select = "select pia.attr_id, (select pa.attr_name from p_attr pa where pa.attr_id = pia.attr_id ) attr_name, "
				+ " pia.val_id, (select pav.val_name from p_attr_val pav where pav.val_id = pia.val_id) val_name, "
				+ " IF((select count(1) from p_sku_attr psa where psa.val_id = pia.val_id and psa.item_id = pia.item_id)>0,1,2) is_have "
				+ " from p_item_attr pia where pia.item_id = %s order by pia.attr_id   ";
		List<Map<String, Object>> list_select = sqlMapper.public_list_all_sql(String.format(sql_select, item_id));
		List<Map<String, Object>> lMaps = new ArrayList<Map<String,Object>>();
		Map<String,Object> map1 = null;
		List<Map<String,Object>> lstMapitems = null;
		String attr_id = "";
		for(Map<String,Object> mapItem: list_select)
		{
			if(attr_id.isEmpty() || !attr_id.equals(mapItem.get("attr_id").toString()))
			{
				map1 = new HashMap<String,Object>();
				attr_id = mapItem.get("attr_id").toString();
				map1.put("attr_id", mapItem.get("attr_id"));
				map1.put("attr_name", mapItem.get("attr_name"));
				lMaps.add(map1);
				
				lstMapitems = new ArrayList<Map<String,Object>>();
				map1.put("items", lstMapitems);
			}
			
			Map<String,Object> mapItem1 = new HashMap<String,Object>(); 
			mapItem1.put("val_id", mapItem.get("val_id"));
			mapItem1.put("val_name", mapItem.get("val_name"));
			lstMapitems.add(mapItem1);
		}
		map.put("selects", lMaps);
		
		
		results.setResult(map);//输出:列表数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:商品管理==》编辑
	//{"mid":"1","action":"b_item_edit_set","item_id":"4","catalog_id":"63","item_name":"青龙蛋","ordid":"100","deadline":"15:20","status":"1","unit":"个","is_sku":"1","uniform_price":"","list":[{"val_ids":"4,5,11","val_names":"青菜,红,1L","uniform_price":"2000000"}]}
	public void b_item_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String item_id = obj_vars.optString("item_id");//
		String catalog_id = obj_vars.optString("catalog_id");//小类id
		String item_name = obj_vars.optString("item_name");//商品名称
		String ordid = obj_vars.optString("ordid");//序号
		String deadline = obj_vars.optString("deadline");//截止时间点
		String status = obj_vars.optString("status");//是否上架 (1上架；2下架)
		String unit = obj_vars.optString("unit");//单位
		String item_small_img = obj_vars.optString("item_small_img");//分类下面的商品小图
		String item_img = obj_vars.optString("item_img"); //商品详情图片
		String item_desc = obj_vars.optString("item_desc");//商品描述
		String is_sku = obj_vars.optString("is_sku");//是否有多规格设置 (0:否;1:是) 0 uniform_price 有值  1 list 有值 
		String uniform_price = obj_vars.optString("uniform_price");//单规格价格
		String liststr = obj_vars.optString("list");//规格信息    list:[{"val_ids":"","val_names":"","uniform_price":""}...]
		
		Map<String, String> map = new HashMap<>();
		map.put("catalog_id", catalog_id);
		map.put("item_name", item_name);
		map.put("ordid", ordid);
		map.put("deadline", deadline);
		map.put("status", status);
		map.put("unit", unit);
		map.put("is_sku", is_sku);
		map.put("item_small_img", item_small_img);
		map.put("item_img", item_img);
		map.put("item_desc", item_desc);
		
		//商品表 p_item
		sqlMapper.insert_or_update("p_item", map, "item_id = "+item_id);
		
		if (Integer.valueOf(is_sku) == 0) { //单规格
			//商品属性值表 p_item_attr
			String sql_item_attr = "insert into p_item_attr (item_id,attr_id,val_id) values (%s,1,1)";
			sqlMapper.insert(String.format(sql_item_attr, item_id));
			//商品规格表  p_sku
			Map<String, String> mapsku = new HashMap<>();
			mapsku.put("item_id", String.valueOf(item_id));
			mapsku.put("val_ids", "1");
			mapsku.put("val_names", "无");
			mapsku.put("uniform_price", uniform_price);
			int sku_id = sqlMapper.insert_or_update("p_sku", mapsku, null);
			//规格属性值表 p_sku_attr
			String sql_sku_attr = "insert into p_sku_attr (item_id,sku_id,attr_id,val_id) values (%s,%s,%s,%s)";
			sqlMapper.insert(String.format(sql_sku_attr, item_id,sku_id,1,1));
		} else {
			List<Integer> list_sku_id = new ArrayList<>();
			
			Gson gson = new Gson();
			List<Map<String, Object>> list = gson.fromJson(liststr, new TypeToken<List<Map<String, Object>>>() {}.getType());
			for (Map<String, Object> map_str : list) {
				String val_ids = map_str.get("val_ids").toString();
				String val_names = map_str.get("val_names").toString();
				String uniform_prices = map_str.get("uniform_price").toString();
				
				String sql_item_val_ids = "select count(1) from p_sku where item_id = %s and val_ids = '%s'";
				int num = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_item_val_ids, item_id,val_ids)));
				
				if (num > 0) { //有值 修改价格
					String sql_sku_id = "select sku_id from p_sku where item_id = %s and val_ids = '%s'";
					int sku_id = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_sku_id,item_id, val_ids)));
					
					String sql_update = "update p_sku set uniform_price = %s where sku_id = %s";
					sqlMapper.update(String.format(sql_update, uniform_prices, sku_id));
					
					list_sku_id.add(sku_id);
				} else { //没有就添加
					String[] strs = val_ids.split(",");
					for (String str : strs) {
						//商品属性值表 p_item_attr
						String sql_item_attr = "insert into p_item_attr (item_id,attr_id,val_id) values (%s,(select attr_id from p_attr_val where val_id = %s),%s)";
						sqlMapper.insert(String.format(sql_item_attr, item_id,Integer.valueOf(str),Integer.valueOf(str)));
					}
					//商品规格表  p_sku
					Map<String, String> mapsku = new HashMap<>();
					mapsku.put("item_id", String.valueOf(item_id));
					mapsku.put("val_ids", val_ids);
					mapsku.put("val_names", val_names);
					mapsku.put("uniform_price", uniform_prices);
					int sku_id = sqlMapper.insert_or_update("p_sku", mapsku, null);
					for (String str : strs) {
						//规格属性值表 p_sku_attr
						String sql_sku_attr = "insert into p_sku_attr (item_id,sku_id,attr_id,val_id) values (%s,%s,(select attr_id from p_attr_val where val_id = %s),%s)";
						sqlMapper.insert(String.format(sql_sku_attr, item_id,sku_id,Integer.valueOf(str),Integer.valueOf(str)));
					}
					list_sku_id.add(sku_id);
				}
			}
			
			//没有对比的规格进行删除操作
			String list_sku_id_str = StringUtils.join(list_sku_id, ",");
			String sql_update_delete = "update p_sku set is_delete = 1 where item_id = %s and sku_id not in (%s) ";
			sqlMapper.update(String.format(sql_update_delete,item_id ,list_sku_id_str));
		}
		
		results.setMessage("修改成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:商品管理==>常用属性设置列表
	//{"mid":"1","action":"b_item_attr_val_list_get","val_name":""}
	public void b_item_attr_val_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String attr_name = obj_vars.optString("attr_name");//属性名称
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql_total_rows = "select count(1) from p_attr ";
		if (!StringUtil.isEmpty(attr_name)) {
			sql_total_rows+= " where attr_name like '%"+attr_name+"%' ";
		}
		String total_rows = sqlMapper.public_one_scale(sql_total_rows);
		
		if (pagenum == 0) {
			pagenum = 1;
		}
		if (pagesize == 0) {
			pagesize = 12;
		}
		int limitStart = (pagenum-1)*pagesize;

		//先分页
		String sql_group_concat = "select ifnull(GROUP_CONCAT(t.attr_id),0) attr_id from (select attr_id from p_attr  ";
				if (!StringUtil.isEmpty(attr_name)) {
					sql_group_concat+= " where attr_name like '%"+attr_name+"%' ";
				}
		        sql_group_concat+= " limit "+limitStart+","+pagesize+") t ";
		String in_nums = sqlMapper.public_one_scale(sql_group_concat);
		
		
		String sql = "SELECT pa.attr_id, pa.attr_name, pav.val_name "
				+ "  FROM p_attr_val pav "
				+ " inner join p_attr pa on pav.attr_id = pa.attr_id "
				+ "  WHERE 1=1  ";
		if (!StringUtil.isEmpty(in_nums)) {
			sql+= " and pa.attr_id in ("+in_nums+") ";
		}
		if (!StringUtil.isEmpty(attr_name)) {
			sql+= " and pa.attr_name like '%"+attr_name+"%' ";
		}
		sql+= " order by pa.attr_id, pav.val_id ";
		
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		List<Map<String,Object>> lstMap = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = null;
		List<Map<String,Object>> lstMapitems = null;
		String attr_id = "";
		for(Map<String,Object> mapItem: list)
		{
			if(attr_id.isEmpty() || !attr_id.equals(mapItem.get("attr_id").toString()))
			{
				map = new HashMap<String,Object>();
				attr_id = mapItem.get("attr_id").toString();
				map.put("attr_id", mapItem.get("attr_id"));
				map.put("attr_name", mapItem.get("attr_name"));
				//订单数据
				lstMap.add(map);
				//订单里面的商品
				lstMapitems = new ArrayList<Map<String,Object>>();
				map.put("items", lstMapitems);
			}
			
			Map<String,Object> mapOrderItem = new HashMap<String,Object>(); 
			mapOrderItem.put("val_name", mapItem.get("val_name"));
			lstMapitems.add(mapOrderItem);
		}
		
		PageModel pageModel = new PageModel();
		pageModel.mapRlt = lstMap;
		pageModel.total_rows = Integer.parseInt(total_rows);
		
		results.setResult(pageModel);//输出:列表数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能：商品管理==>常用属性设置列表==>修改属性查询
	public void b_item_attr_val_edit_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int attr_id = obj_vars.optInt("attr_id");//常用属性id
		
		String sql_attr = "select pa.attr_id, pa.catalog_id catalog_parent_id, pa.attr_name from p_attr pa where pa.attr_id = %s";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql_attr, attr_id));
		
		String sql = "select val_id, val_name from p_attr_val where attr_id = %s";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sql, attr_id));
		
		map.put("list", list);
		
		results.setResult(map);//输出:列表数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能：商品管理==>常用属性设置列表==>修改属性
	public void b_item_attr_val_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int attr_id = obj_vars.optInt("attr_id");//常用属性id
		String liststr = obj_vars.optString("list");//list:["val_id":"","val_name":"",...] val_id 没有就给空
		
		List<Integer> val_ids = new ArrayList<>();//修改的 val_id集合
		
		Gson gson = new Gson();
		List<Map<String, String>> list = gson.fromJson(liststr, new TypeToken<List<Map<String, String>>>() {}.getType());
		for (Map<String, String> map : list) {
			Object val_id = map.get("val_id");
			Object val_name = map.get("val_name");
			
			if (val_id != null && !val_id.equals("")) {//不为空 修改
				String sql_update = "update p_attr_val set val_name = '%s' where val_id = %s";
				sqlMapper.update(String.format(sql_update, val_name,val_id));
				
				val_ids.add(Integer.valueOf(val_id.toString()));
				
			} else {//为空  新增
				Map<String, String> map_insert = new HashMap<>();
				map_insert.put("val_name", val_name.toString());
				map_insert.put("attr_id", String.valueOf(attr_id));
				
				int val_id_insert = sqlMapper.insert_or_update("p_attr_val", map_insert, null);
				val_ids.add(val_id_insert);
			}
		}
		
		
		//没有的删除
		String val_ids_str = StringUtils.join(val_ids, ",");
		String sql_delete = "delete from p_attr_val where attr_id = %s and val_id not in ("+val_ids_str+")";
		sqlMapper.delete(String.format(sql_delete, attr_id));
		
		Map<String, Object> map = new HashMap<>();
		map.put("attr_id", attr_id);
		map.put("list", list);
		
		results.setResult(map);//输出:列表数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	
	//接口功能:商品管理==>常用属性设置列表==>新增属性(添加一个大类的id)
	public void b_item_attr_val_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类的id
		String attr_name = obj_vars.optString("attr_name");//属性名称
		String liststr = obj_vars.optString("list"); // list:["a","b"...] 属性值
		
		//新增属性
		Map<String, String> map = new HashMap<>();
		map.put("catalog_id", catalog_parent_id);
		map.put("attr_name", attr_name);
		int attr_id = sqlMapper.insert_or_update("p_attr", map, null);
		
		if (!StringUtil.isEmpty(liststr)) {
			Gson gson = new Gson();
			List<String> list = gson.fromJson(liststr, new TypeToken<List<String>>() {}.getType());
			for (String str : list) {
				String sql = "insert into p_attr_val (val_name, attr_id) values ('%s',%s)";
				sqlMapper.insert(String.format(sql, str,attr_id));
			}
		} else {
			String sql = "insert into p_attr_val (val_name, attr_id) values ('无',%s)";
			sqlMapper.insert(String.format(sql, attr_id));
		}
		
		results.setMessage("新增成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户模糊查询
	//{"mid":"1","action":"b_company_like_get","company":"武汉"}
	public void b_company_like_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String kehu_company = obj_vars.optString("kehu_company");//模糊查询客户名称
		
		String sql = "select cid, company from p_customer where 1=1 ";
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and company like '%"+kehu_company+"%' ";
		}
		sql+= " order by cid ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		results.setResult(list);//输出:客户信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:订单列表
	//{"mid":"1","action":"b_order_list_get","status":"1","order_no":"","start_time":"2018-9-20","end_time":"2018-10-10","company":"","member_name":"","diff_amount":"1","pagenum":"0","pagesize":"0"}
	public void b_order_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String status = obj_vars.optString("status"); //订单状态 (1:待收货2:已完成3:全部)
		String order_no = obj_vars.optString("order_no");//订单编号
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String member_name = obj_vars.optString("member_name");//业务员名称
		String diff_amount = obj_vars.optString("diff_amount");//差额:1:为0 2:小于0 3:大于0
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT po.oid, po.cid, po.company kehu_company, pc.contact_name, pc.contact_phone, hm.member_name salesman_name, DATE_FORMAT(po.create_time,'%Y-%m-%d %H:%i:%m') create_time  "
				+ "  , po.order_no, po.status, "
				+ "  ifnull(po.real_pay_amount,0) real_pay_amount, "
				+ "  ifnull(po.virtual_pay_amount,0) virtual_pay_amount, "
				+ "  ifnull(po.diff_amount,0) diff_amount, "
				+ "  DATE(po.send_time) send_time "
				+ "  FROM p_order po  "
				+ "  left join p_customer pc on po.cid = pc.cid "
				+ "  left join hx_member  hm on pc.salesman_id = hm.member_id"
				+ "  WHERE 1=1 and po.is_history = 0 "; //is_history 是否历史订单：0正常；1用户删除；2后台删除
		if (!StringUtil.isEmpty(status)) {
			switch (status) {
			case "1":
				sql+= " and po.status = '待收货' ";
				break;
			case "2":
				sql+= " and po.status = '已完成' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(order_no)) {
			sql+= " and po.order_no = '"+order_no+"' ";
		}
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(po.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(po.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and po.company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(member_name)) {
			sql+= " and hm.member_name like '%"+member_name+"%' ";
		}
		if (!StringUtil.isEmpty(diff_amount)) {
			switch (diff_amount) {
			case "1":
				sql+= " and po.diff_amount = 0 ";
				break;
			case "2":
				sql+= " and po.diff_amount < 0 ";
				break;
			case "3":
				sql+= " and po.diff_amount > 0 ";
				break;
			}
		}
		sql+= " order by po.create_time desc  ";
		
		System.out.println(sql);
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		BigDecimal real_sum = new BigDecimal(0);
		BigDecimal virtual_sum = new BigDecimal(0);
		BigDecimal diff = new BigDecimal(0);
		for (Map<String, Object> map : pageModel.mapRlt) {
			Object real_pay_amount = map.get("real_pay_amount");
			Object virtual_pay_amount = map.get("virtual_pay_amount");
			if (real_pay_amount != null && !real_pay_amount.equals("")) {
				real_sum = real_sum.add(new BigDecimal(map.get("real_pay_amount").toString()));
			}
			if (virtual_pay_amount != null && !virtual_pay_amount.equals("")) {
				virtual_sum = virtual_sum.add(new BigDecimal(map.get("virtual_pay_amount").toString()));
			}
			//是否有虚拟订单
			String sql_virtual = "select count(1) from p_order_detail where oid = "+map.get("oid")+" and order_type = 2";
			int is_virtual = Integer.valueOf(sqlMapper.public_one_scale(sql_virtual));
			
			if (is_virtual <= 0) { //没有虚拟订单
				map.put("virtual_pay_amount", 0);
			} 
		}
		
		diff = real_sum.subtract(virtual_sum);
		Map<String, Object> map = new HashMap<>();
		map.put("real_sum", real_sum);
		map.put("virtual_sum", virtual_sum);
		map.put("diff", diff);
		pageModel.data = map;
		
		results.setResult(pageModel);//输出:订单信息 real_sum 实际总计  virtual_sum 虚拟总计  diff 差额
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:订单详情
	//{"mid":"1","action":"b_order_detail_get","oid":"14"}
	public void b_order_detail_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int oid = obj_vars.optInt("oid");//订单id
		
		String sqldetail = " select po.oid, po.company kehu_company, pc.contact_name, pc.contact_phone, hm.member_name salesman_name, DATE_FORMAT(po.create_time,'%Y-%m-%d %H:%i:%m') create_time   "
				+ " , po.order_no, po.status, po.real_pay_amount, po.virtual_pay_amount, ifnull(po.diff_amount,0) diff_amount, DATE(po.send_time) send_time , "
				+ " pc.district_name, pc.address ,hdm.member_name deliver_name, po.order_note  "
				+ " from p_order po "
				+ " left join p_customer pc on po.cid = pc.cid "
				+ " left join hx_member  hm on pc.salesman_id = hm.member_id  "
				+ " left join hx_member  hdm on pc.deliver_id = hdm.member_id "
				+ " where po.oid = "+oid+"  ";
		Map<String, Object> mapdetail = sqlMapper.public_one_detail(sqldetail);
		
		//实际订单
		String sql_real = "select poi.order_item_id, (select pc.catalog_name from p_catalog pc where pc.catalog_id = pca.pid) catalog_parent_name, "
				+ " pca.catalog_name, poi.item_name,  "
				+ " (case poi.oi_type when 1 then '销售' when 2 then '送货' when 3 then '上单' end) diff_type, "
				+ " poi.val_names, poi.unit, poi.customer_note, poi.order_num, poi.actual_num, poi.pay_price, (poi.pay_price*poi.actual_num) sum,"
				+ " (case when poi.source_type = 1 then '库存' when poi.source_type = 2 then (select company from p_supply where sid = poi.source_id ) "
				+ " when poi.source_type = 3 then (select member_name from hx_member where member_id = poi.source_id) end) company_name , "
				+ " poi.actual_num provide_num, poi.cost_price, (poi.actual_num*poi.cost_price) cost_sum, poi.last_update_note "
				+ " from p_order_item poi "
				+ " left join p_order_detail pod on poi.detail_oid = pod.detail_oid "
				+ " left join p_item pi on poi.item_id = pi.item_id "
				+ " left join p_catalog pca on pi.catalog_id = pca.catalog_id "
				+ " where pod.oid = "+oid+" and pod.order_type = 1 order by poi.order_item_id  ";
		
		List<Map<String, Object>> list_real = sqlMapper.public_list_all_sql(sql_real);
		System.out.println(sql_real);
		
		//实际订单操作记录
		String sql_real_remarks = "select DATE_FORMAT(hp.create_time,'%%Y-%%m-%%d %%H:%%i:%%m') create_time, "
				+ " (case hp.update_type when 1 then '修改人' when 2 then '操作人' when 3 then '打印人' end) update_type, "
				+ " (select member_name from hx_member where member_id = hp.member_id ) member_name ,description "
				+ "  from hx_print hp where hp.print_type = %s and ref_id = %s order by hp.create_time desc limit 10 ";
		List<Map<String, Object>> list_real_remarks = sqlMapper.public_list_all_sql(String.format(sql_real_remarks, PrintEnum.real.getId(),oid));
		
		mapdetail.put("list_real", list_real);
		mapdetail.put("list_real_remarks", list_real_remarks);
		
		//是否有虚拟订单
		String sql_virtual = "select count(1) from p_order_detail where oid = "+oid+" and order_type = 2";
		int is_virtual = Integer.valueOf(sqlMapper.public_one_scale(sql_virtual));
		
		if (is_virtual <= 0) {
			mapdetail.put("is_virtual", "2");
			mapdetail.put("virtual_pay_amount", "0");
			mapdetail.put("diff_amount", "0");
		} else {
			mapdetail.put("is_virtual", "1");
		}
		
		results.setResult(mapdetail);//输出:订单详细信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:订单详情==>通知供应商
	public void b_order_detail_notice() throws IOException ,SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int oid = obj_vars.optInt("oid");//订单id
		
		//查询供应商id
		String sql = "select poi.source_id from p_order_item poi  "
				+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
				+ " where pod.order_type = 1 and poi.source_type = 2 and pod.oid = %s "
				+ " group by poi.source_id order by poi.source_id ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sql, oid));
		
		//订单编号
		String sql_order_no = "select order_no from p_order where oid = %s";
		String order_no = sqlMapper.public_one_scale(String.format(sql_order_no, oid));
		
		String msg_value = "您的编号为 "+order_no+" 的订单已被修改,请及时查看";
		String title = "订单修改";
		String content = "您的编号为 "+order_no+" 的订单已被修改,请及时查看";
		for (Map<String, Object> map : list) {
			int source_id = Integer.valueOf(map.get("source_id").toString());
			send_message(MsgTypeEnum.system_order.getId(), msg_value,source_id,title,content);
		}
		
		results.setMessage("通知成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:订单详情==>实际订单==>打印
	public void b_order_detail_print_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int oid = obj_vars.optInt("oid");//订单id
		
		List<Map<String, Object>> list = null;
		
		//获取客户的制单格式
		String sql_bill_format_type = "select IFNULL((select bill_format_type from p_customer where cid = po.cid),1) bill_format_type "
				+ " from p_order po where po.oid = %s ";
		int bill_format_type = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_bill_format_type, oid)));
		
		if(bill_format_type == FormatEnum.total.getId()) {
			list = total_print(oid,OrderTypeEnum.real.getId());
		}
		if(bill_format_type == FormatEnum.classes.getId()) {
			list = classes_print(oid,OrderTypeEnum.real.getId());
		}
		if(bill_format_type == FormatEnum.order.getId()) {
			list = order_print(oid,OrderTypeEnum.real.getId());
		}
		
		//添加打印记录
		print_set(member_id, PrintEnum.real.getId(), oid,UpdateTypeEnum.defaults.getId());
		
		results.setResult(list);//输出:订单详细信息  多个表格  number 表格数量     list_1 .... 表格数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:订单详情==>虚拟订单==>打印
	public void b_order_detail_virtual_print_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int oid = obj_vars.optInt("oid");//订单id
		
		List<Map<String, Object>> list = null;
		
		//获取客户的制单格式
		String sql_bill_format_type = "select IFNULL((select bill_format_type from p_customer where cid = po.cid),1) bill_format_type "
				+ " from p_order po where po.oid = %s ";
		int bill_format_type = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_bill_format_type, oid)));
		
		if(bill_format_type == FormatEnum.total.getId()) {
			list = total_print(oid,OrderTypeEnum.virtual.getId());
		}
		if(bill_format_type == FormatEnum.classes.getId()) {
			list = classes_print(oid,OrderTypeEnum.virtual.getId());
		}
		if(bill_format_type == FormatEnum.order.getId()) {
			list = order_print(oid,OrderTypeEnum.virtual.getId());
		}
		
		//添加打印记录
		print_set(member_id, PrintEnum.virtual.getId(), oid,UpdateTypeEnum.defaults.getId());
		
		results.setResult(list);//输出:订单详细信息  多个表格  number 表格数量     list_1 .... 表格数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:订单列表==>订单详情==>获取虚拟订单
	//{"mid":"1","action":"b_order_detail_virtual_get","oid":"75"}
	public void b_order_detail_virtual_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int oid = obj_vars.optInt("oid");//订单id
		
		//修改商品订单状态
		String sql_update_oi_type = "update p_order_item set oi_type = %s "
				+ "where detail_oid = (select detail_oid from p_order_detail where oid = %s and order_type = 1)";
		sqlMapper.update(String.format(sql_update_oi_type, OiTypeEnum.delivery.getId(),oid));
		
		//判断有没有虚拟订单
		String sql = "select count(1) from p_order_detail where oid = "+oid+" and order_type = 2";
		String count = sqlMapper.public_one_scale(sql);
		if (Integer.valueOf(count) <= 0) {
			
			//增加虚拟订单
			String sql_real = "select oid,detail_oid, cost_amount,uniform_amount,customer_amount,"
					+ "pay_amount,item_num from p_order_detail where oid = %s and order_type = 1";
			Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql_real, oid));
			
			Map<String, String> map_str = new HashMap<>();
			map_str.put("oid", map.get("oid").toString());
			map_str.put("cost_amount", map.get("cost_amount").toString());
			map_str.put("uniform_amount", map.get("uniform_amount").toString());
			map_str.put("customer_amount", map.get("customer_amount").toString());
			map_str.put("pay_amount", map.get("pay_amount").toString());
			map_str.put("item_num", map.get("item_num").toString());
			map_str.put("order_type", "2");
			
			int detail_id = sqlMapper.insert_or_update("p_order_detail", map_str, null);
			
			String sql_add = "insert into p_order_item (detail_oid,item_id,sku_id,oi_type,order_num,"
					+ "actual_num,cost_price,uniform_price,customer_price,pay_price,money,sum_cost, "
					+ " customer_note,item_name,item_small_img,val_names,unit,source_type,source_id) "
					+ " select "+detail_id+", item_id,sku_id,3,order_num,actual_num,cost_price,uniform_price,"
					+ "customer_price,pay_price,money,sum_cost, "
					+ " customer_note,item_name,item_small_img,val_names,unit,"
					+ "source_type,source_id from p_order_item where detail_oid = "+map.get("detail_oid").toString()+" ";
			
			sqlMapper.insert(sql_add);
			
			//修改订单实际订单属性
			String sql_update_real = "update p_order_item set oi_type = 2 where detail_oid = "
					+ "(select detail_oid from p_order_detail where oid = %s and order_type = 1)";
			sqlMapper.update(String.format(sql_update_real, oid));
		} 
		
		Map<String, Object> map = new HashMap<>();
		
		//虚拟订单
		String sql_virtual = "select poi.order_item_id, (select pc.catalog_name from p_catalog pc where pc.catalog_id = pca.pid) catalog_parent_name, "
				+ " pca.catalog_name, poi.item_name, "
				+ " (case poi.oi_type when 1 then '销售' when 2 then '送货' when 3 then '上单' end) diff_type, "
				+ " poi.val_names, poi.unit, poi.customer_note, poi.order_num, poi.actual_num, poi.pay_price, (poi.pay_price*poi.actual_num) sum,"
				+ " (case when poi.source_type = 1 then '库存' when poi.source_type = 2 then (select company from p_supply where sid = poi.source_id ) "
				+ " when poi.source_type = 3 then (select member_name from hx_member where member_id = poi.source_id) end) company_name , "
				+ " poi.actual_num provide_num, poi.cost_price, (poi.actual_num*poi.cost_price) cost_sum, poi.last_update_note "
				+ " from p_order_item poi "
				+ " left join p_order_detail pod on poi.detail_oid = pod.detail_oid "
				+ " left join p_item pi on poi.item_id = pi.item_id "
				+ " left join p_catalog pca on pi.catalog_id = pca.catalog_id "
				+ " where pod.oid = "+oid+" and pod.order_type = 2 order by poi.order_item_id  ";
		List<Map<String, Object>> list_virtual = sqlMapper.public_list_all_sql(sql_virtual);
		
		//虚拟订单操作记录
		String sql_virtual_remarks = "select DATE_FORMAT(hp.create_time,'%%Y-%%m-%%d %%H:%%i:%%m') create_time, "
				+ " (case hp.update_type when 1 then '修改人' when 2 then '操作人' when 3 then '打印人' end) update_type, "
				+ " (select member_name from hx_member where member_id = hp.member_id ) member_name ,description "
				+ "  from hx_print hp where hp.print_type = %s and ref_id = %s order by hp.create_time desc limit 10 ";
		List<Map<String, Object>> list_virtual_remarks = sqlMapper.public_list_all_sql(String.format(sql_virtual_remarks, PrintEnum.virtual.getId(), oid));
		
		map.put("list_virtual", list_virtual);
		map.put("list_virtual_remarks", list_virtual_remarks);
		
		results.setResult(map);//输出:虚拟订单详细信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:订单列表==>确认订单
	//{"mid":"1","action":"b_order_confirm_set","oid":"3"}
	public void b_order_confirm_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int oid = obj_vars.optInt("oid");
		
		String sql = "update p_order set status = '已完成', queren_member_id = %s, create_time = now() where oid = %s";
		sqlMapper.update(String.format(sql, member_id, oid));
		
		results.setMessage("确认成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:订单列表==>实际订单==>新增商品==>获取子类下的商品
	//{"mid":"1","action":"b_order_catalog_item_get","catalog_id":"13"}
	public void b_order_catalog_item_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int catalog_id = obj_vars.optInt("catalog_id"); //子类id
		
		String sql ="select item_id, item_name, unit from p_item where catalog_id = %s and is_delete = 2 and status = 1 order by item_id";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sql, catalog_id));
		
		results.setResult(list);//输出:商品信息
		results.setMessage("确认成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:订单列表==>实际订单==>新增商品==>获取配送员列表信息
	//{"mid":"1","action":"b_order_deliver_get"}
	public void b_order_deliver_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		
		String sql = "select hrm.member_id, (select hm.member_name from hx_member hm where hm.member_id = hrm.member_id )"
				+ " member_name from hx_role_member hrm where hrm.role_id = %s order by hrm.member_id";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sql, RoleEnum.deliver.getId()));
		
		results.setResult(list);//输出:配送员信息
		results.setMessage("确认成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:订单列表==>实际订单==>新增商品==>获取供应商列表信息
	//{"mid":"1","action":"b_order_supply_get","item_id":"8","sku_id":"18"}
	public void b_order_supply_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int item_id = obj_vars.optInt("item_id");//商品id
		int sku_id = obj_vars.optInt("sku_id");//商品规格id
		
		String sql = "select psi.sid, ps.company from p_supply_item psi"
				+ "   left join p_supply ps on ps.sid = psi.sid "
				+ "   left join p_sku psku on psku.sku_id = psi.sku_id "
				+ "   where psi.item_id = %s and psi.sku_id = %s and ps.supply_type != 2 and psku.is_delete = 2 "
				+ "   group by psi.sid  order by psi.sid ";
        List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sql, item_id, sku_id));
		
		results.setResult(list);//输出:供应商信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:客户列表==>自动派单设置==>添加查询
	public void b_supply_automatic_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int catalog_id = obj_vars.optInt("catalog_id");//小类id
		
		String sql = "select psi.sid, (select ps.company from p_supply ps where ps.sid = psi.sid ) "
				+ "company from p_supply_item psi where psi.catalog_id = %s group by psi.sid ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sql, catalog_id));
		
		results.setResult(list);//输出:供应商信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:订单列表==>实际订单==>新增商品==>获取商品规格信息
	//{"mid":"1","action":"b_order_item_sku_get","item_id":"3"}
	public void b_order_item_sku_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int item_id = obj_vars.optInt("item_id");//商品id
		int customer_id = obj_vars.optInt("customer_id");//客户id
		
		String sql = "select sku_id, val_names from p_sku where item_id = %s and is_delete = 2 order by sku_id";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sql, item_id));
		
		for (Map<String, Object> map : list) {
			int sku_id = Integer.valueOf(map.get("sku_id").toString());
			String sql_customer_price = "select customer_price from p_price_temp_detail where sku_id = %s "
					+ " and price_temp_id = (select price_temp_id from p_customer where cid = %s)";
			String customer_price = sqlMapper.public_one_scale(String.format(sql_customer_price, sku_id,customer_id));
			
			if (StringUtil.isEmpty(customer_price)) {
				//售价
				String sql_uniform_price = "select uniform_price from p_item where item_id = %s";
				String uniform_price = sqlMapper.public_one_scale(String.format(sql_uniform_price, item_id));
				map.put("uniform_price", uniform_price);
			} else {
				map.put("uniform_price", customer_price);
			}
		}
		
		results.setResult(list);//输出:商品规格信息
		results.setMessage("确认成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:订单列表==>实际订单==>新增商品==>获取规格相应成本价格
	public void b_order_item_add_price_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int type = obj_vars.optInt("type");//类型 1是库存，2是供应商
		int sku_id = obj_vars.optInt("sku_id");//规格id
		int supply_id = obj_vars.optInt("supply_id");//供应商id
		
		String cost_price = "";
		
		if (type == 2) {// 供应商
			String sql = "select purchase_price from p_supply_item where sku_id = %s and sid = %s";
			cost_price = sqlMapper.public_one_scale(String.format(sql, sku_id, supply_id));
		} else {
			String sql = "select cost_price from hx_inventory where sku_id = %s";
			cost_price = sqlMapper.public_one_scale(String.format(sql, sku_id));
		}
		
		results.setResult(cost_price);//输出:成本价  可能为空 为空提示 不提交
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	
	//接口功能:订单列表==>实际订单==>新增商品
	public void b_order_detail_real_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int oid = obj_vars.optInt("oid");//订单id
		String catalog_id = obj_vars.optString("catalog_id");//小类id
		String item_id = obj_vars.optString("item_id");//商品id
		String sku_id = obj_vars.optString("sku_id");//规格信息 
		int order_num = obj_vars.optInt("order_num");//订单数量
		String uniform_price = obj_vars.optString("uniform_price");//售价
		String money = obj_vars.optString("money");//金额
		String customer_note = obj_vars.optString("customer_note");//客户备注 source_type供货来源类型：1库存；2供应商；3临采师傅  source_id 相应id actual_num 提供数量 cost_price 成本价
		String liststr = obj_vars.optString("list");// 来源相关 list:[{"source_type":"","source_id":"","actual_num":"","cost_price":"","sum_cost":""}...]   sum_cost 总成本

		//查询订单详情id
		String sql_detail_oid = "select detail_oid from p_order_detail where oid = %s and order_type = 1";
		String detail_oid = sqlMapper.public_one_scale(String.format(sql_detail_oid, oid));
		
		//判断是否有过备份(如果没有就备份) 真实订单
		if_first_real(oid);
		
		//查询商品详情
		String sql_item_ = "select item_name, item_small_img, uniform_price, unit from p_item where item_id = %s";
		Map<String, Object> map_item = sqlMapper.public_one_detail(String.format(sql_item_, item_id));
		
		//获取规格名称
		String sql_sku_val_names = "select val_names from p_sku where sku_id = %s";
		String val_names = sqlMapper.public_one_scale(String.format(sql_sku_val_names, sku_id));
		
		int order_nums = 0;
		
		Gson gson = new Gson();
		List<Map<String, String>> list = gson.fromJson(liststr, new TypeToken<List<Map<String, String>>>() {}.getType());
		for (Map<String, String> map : list) {
			Map<String, String> map_order_item = new HashMap<>();
			
			map_order_item.put("detail_oid",detail_oid);
			map_order_item.put("item_id", item_id);
			map_order_item.put("sku_id", sku_id);
			map_order_item.put("order_num", String.valueOf(order_num));//商品数量，下单的时的商品数量
			map_order_item.put("actual_num", map.get("actual_num"));//真实数量，后台改单之后的数量，没改之前和order_num一致
			map_order_item.put("cost_price", map.get("cost_price"));//成本价：直接到供应商和师傅临采的的采购价就是成本价，如果是入库的成本价是平均成本价
			map_order_item.put("sum_cost", map.get("sum_cost"));//总成本
			map_order_item.put("uniform_price", map_item.get("uniform_price").toString());//统一售价
			map_order_item.put("customer_price", uniform_price);//客户售价
			map_order_item.put("pay_price", uniform_price);//售价，如果有客户售价则是客户售价否则是标准售价
			map_order_item.put("money", money);//金额
			map_order_item.put("customer_note", customer_note);//客户备注
			map_order_item.put("last_update_note", customer_note);//用来累计备注用的
			map_order_item.put("item_name", map_item.get("item_name").toString());
			if (map_item.get("item_small_img") != null) {
				map_order_item.put("item_small_img", map_item.get("item_small_img").toString());
			}
			map_order_item.put("unit", map_item.get("unit").toString());
			map_order_item.put("val_names", val_names);
			map_order_item.put("source_type", map.get("source_type"));
			if (Integer.valueOf(map.get("source_type")) == 1) {//库存
				map_order_item.put("source_id", "0");
			} else {
				map_order_item.put("source_id", map.get("source_id"));
			}
			//是否有虚拟订单
			String sql_virtual = "select count(1) from p_order_detail where oid = "+oid+" and order_type = 2";
			int is_virtual = Integer.valueOf(sqlMapper.public_one_scale(sql_virtual));
			
			if (is_virtual <= 0) {
				map_order_item.put("oi_type", String.valueOf(OiTypeEnum.sales.getId()));
			} else {
				map_order_item.put("oi_type", String.valueOf(OiTypeEnum.delivery.getId()));
			}
			
			sqlMapper.insert_or_update("p_order_item", map_order_item, null);
		}
		
		//新增记录
		String record= "增加菜品【"+map_item.get("item_name").toString()+"】 规格【"+val_names+"】 数量 "+order_num+"";
		
		String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s, update_type = %s, ref_id = %s ,description = '%s'";
		sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.real.getId(),UpdateTypeEnum.update.getId(),oid,record));
		
		//更新订单 备份订单 (真实订单)
		update_copy_real(member_id,oid);
		
		//订单修改通知
		order_update_notice(oid);
		
		//更新订单差异
		update_order_diff(oid);
		
		results.setMessage("新增成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:订单列表==>真实订单==>修改订单查询
	//{"mid":"1","action":"b_order_update_get","oid":"3"}
	public void b_order_update_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int oid = obj_vars.optInt("oid");//订单id
		
		String sqldetail = " select po.oid,  DATE_FORMAT(po.send_time,'%Y-%m-%d %H:%i:%m') "
				+ " send_time, po.diff_amount , po.charge_amount, (po.charge_amount+po.diff_amount) diff_charge from p_order po  where po.oid = "+oid+" ";
		Map<String, Object> mapdetail = sqlMapper.public_one_detail(sqldetail);
		
		//是否可以冲账 1:可以冲账 2:不可以冲账
		String is_charge = ""; 
		//差额为正或负数 1:正 2:负
		String plus_minus = "";
		//冲账信息
		BigDecimal diff_charge = new BigDecimal(mapdetail.get("diff_charge").toString());
		if (diff_charge.compareTo(new BigDecimal(0)) == 1) {
			is_charge = "1";
			plus_minus = "1";
		} else if(diff_charge.compareTo(new BigDecimal(0)) == -1) {
			is_charge = "1";
			plus_minus = "2";
		} else {
			is_charge = "2";
		}
		
		
		Map<String, Object> map = new HashMap<>();
		map.put("mapdetail", mapdetail);
		map.put("is_charge", is_charge);
		map.put("plus_minus", plus_minus);
		
		results.setResult(map);//输出:订单信息 list_real 订单信息  is_charge 1:可以冲账 2:不可以冲账 plus_minus 差额为正或负数 1:正 2:负
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:订单列表==>真实订单==>修改订单==>获取冲账订单详情
	//{"mid":"1","action":"b_order_charge_detail_get","oid":"1"}
	public void b_order_charge_detail_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int oid = obj_vars.optInt("oid");//订单id
		int order_no = obj_vars.optInt("order_no");//订单号码
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		//获取订单信息
		String sqldetail = " select po.oid,  DATE_FORMAT(po.send_time,'%Y-%m-%d %H:%i:%m') "
				+ " send_time, po.diff_amount , po.charge_amount, (po.charge_amount+po.diff_amount) diff_charge from p_order po  where po.oid = "+oid+" ";
		Map<String, Object> mapdetail = sqlMapper.public_one_detail(sqldetail);
		
		//是否可以冲账 1:可以冲账 2:不可以冲账
		String is_charge = ""; 
		//差额为正或负数 1:正 2:负
		String plus_minus = "";
		String sql_charge = "";
		
		BigDecimal diff_charge = new BigDecimal(mapdetail.get("diff_charge").toString());
		if (diff_charge.compareTo(new BigDecimal(0)) == 1) {
			is_charge = "1";
			plus_minus = "1";
			sql_charge = "SELECT oid, order_no, real_pay_amount, virtual_pay_amount,"
					+ " (diff_amount+charge_amount) diff_charge FROM p_order WHERE (diff_amount+charge_amount) < 0";
		} else if(diff_charge.compareTo(new BigDecimal(0)) == -1) {
			is_charge = "1";
			plus_minus = "2";
			sql_charge = "SELECT oid, order_no, real_pay_amount, virtual_pay_amount,"
					+ " (diff_amount+charge_amount) diff_charge FROM p_order WHERE (diff_amount+charge_amount) > 0";
		} else {
			is_charge = "2";
		}
		
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql_charge, pagenum);
		
		results.setResult(pageModel);//输出:订单信息 
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:订单列表==>真实订单==>修改订单
	//{"mid":"1","action":"b_order_update_set","oid":"1","send_time":"2018-05-09 12:12:30","list_order_item":[{"order_item_id":"1","actual_num":"10"},{"order_item_id":"2","actual_num":"20"}],"list_charge":[{"oid":"2","charge_amount":"-8"}]}
	public void b_order_update_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int oid = obj_vars.optInt("oid");//订单id
		String send_time = obj_vars.optString("send_time");//配送时间
		String list_order_item_str = obj_vars.optString("list_order_item");//订单数量集合 list_order_item [{"order_item_id":"","actual_num":""},...]provide_num 提供数量
		String list_charge_str = obj_vars.optString("list_charge");//冲账集合  list_charge [{"oid":"","diff_charge_amount":""},...]
		
		Gson gson = new Gson();
		List<Map<String, String>> list_charge = gson.fromJson(list_charge_str, new TypeToken<List<Map<String, String>>>() {}.getType());
		//判断冲账金额是否相符
		//gengeks
		
		//判断是否有过备份(如果没有就备份) 真实订单
		if_first_real(oid);
		
		//获取订单信息
		String sqldetail = " select po.oid,  DATE_FORMAT(po.send_time,'%Y-%m-%d %H:%i:%m') "
				+ " send_time, po.diff_amount , po.charge_amount, (po.charge_amount+po.diff_amount) diff_charge from p_order po  where po.oid = "+oid+" ";
		Map<String, Object> mapdetail = sqlMapper.public_one_detail(sqldetail);
		
		//冲账相关
		BigDecimal diff_charge_sum = new BigDecimal(0);
		
		//p_order_charge
		for (Map<String, String> map_charge : list_charge) {
			diff_charge_sum = diff_charge_sum.add(new BigDecimal(map_charge.get("diff_charge_amount")));
			Map<String, String> map = new HashMap<>();
			map.put("oid", String.valueOf(oid));
			map.put("rel_oid", map_charge.get("oid"));
			map.put("amount", map_charge.get("diff_charge_amount"));
			
			sqlMapper.insert_or_update("p_order_charge", map, null);
			
			//作为关联点单冲账
			String sql = "update p_order set rel_charge_amount = rel_charge_amount+"+Integer.valueOf(map_charge.get("diff_charge_amount"))+" "
					+ ", charge_amount = charge_amount+"+Integer.valueOf(map_charge.get("diff_charge_amount"))+" where oid = "+oid+"";
			sqlMapper.update(sql);
			
		}
		//插入金额
		diff_charge_sum = diff_charge_sum.add(new BigDecimal(mapdetail.get("charge_amount").toString()));
				
		//修改记录
		String record= "";
		
		//修改商品信息数量信息 p_order_item
		List<Map<String, String>> list_order_item = gson.fromJson(list_order_item_str, new TypeToken<List<Map<String, String>>>() {}.getType());
		for (Map<String, String> map : list_order_item) {
			//获取商品名称
			String sql_item_name_actual_num = "select (select item_name from p_item where item_id = poi.item_id) item_name, poi.actual_num "
					+ "from p_order_item  poi where poi.order_item_id = %s";
			Map<String, Object> map_record = sqlMapper.public_one_detail(String.format(sql_item_name_actual_num, map.get("order_item_id").toString()));
			
			record+= "修改菜品【"+map_record.get("item_name")+"】数量   修改前【"+map_record.get("actual_num")+"】 修改后【"+map.get("actual_num")+"】   ";
			//更新
			Integer actual_num = Integer.valueOf(map.get("actual_num").toString());
			Integer order_item_id = Integer.valueOf(map.get("order_item_id").toString());
			String sql_update_order_item = " update p_order_item set actual_num = %s ,sum_cost = (cost_price * %s) where order_item_id = %s ";
			sqlMapper.update(String.format(sql_update_order_item, actual_num,actual_num,order_item_id));
		}
		
		String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s, update_type = %s, ref_id = %s ,description = '%s'";
		sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.real.getId(),UpdateTypeEnum.update.getId(),oid,record));
		
		//更新订单 备份订单 (真实订单)
		update_copy_real(member_id,oid);
		
		//更新订单信息
		String sql_update_order = "update p_order set  send_time = '%s',charge_amount = %s  where oid = %s ";
		sqlMapper.update(String.format(sql_update_order, send_time,diff_charge_sum,oid));
		
		//订单修改通知
		order_update_notice(oid);
		
		//更新订单差异
		update_order_diff(oid);
		
		results.setMessage("修改成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:订单列表==>虚拟订单==>新增商品
	public void b_order_detail_virtual_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int oid = obj_vars.optInt("oid");//订单id
		String catalog_id = obj_vars.optString("catalog_id");//小类id
		String item_id = obj_vars.optString("item_id");//商品id
		String sku_id = obj_vars.optString("sku_id");//规格信息 
		int order_num = obj_vars.optInt("order_num");//订单数量
		String uniform_price = obj_vars.optString("uniform_price");//售价
		String money = obj_vars.optString("money");//金额
		String customer_note = obj_vars.optString("customer_note");//客户备注 source_type供货来源类型：1库存；2供应商；3临采师傅  source_id 相应id actual_num 提供数量 cost_price 成本价
		String liststr = obj_vars.optString("list");// 来源相关 list:[{"source_type":"","source_id":"","actual_num":"","cost_price":"","sum_cost":""}...]   sum_cost 总成本

		//查询订单详情id
		String sql_detail_oid = "select detail_oid from p_order_detail where oid = %s and order_type = 2";
		String detail_oid = sqlMapper.public_one_scale(String.format(sql_detail_oid, oid));
		
		//判断是否有过备份(如果没有就备份) 虚拟订单
		if_first_virtual(oid);
		
		//查询商品详情
		String sql_item_ = "select item_name, item_img, uniform_price, unit from p_item where item_id = %s";
		Map<String, Object> map_item = sqlMapper.public_one_detail(String.format(sql_item_, item_id));
		
		//获取规格名称
		String sql_sku_val_names = "select val_names from p_sku where sku_id = %s";
		String val_names = sqlMapper.public_one_scale(String.format(sql_sku_val_names, sku_id));
		
		Gson gson = new Gson();
		List<Map<String, String>> list = gson.fromJson(liststr, new TypeToken<List<Map<String, String>>>() {}.getType());
		for (Map<String, String> map : list) {
			Map<String, String> map_order_item = new HashMap<>();
			
			map_order_item.put("detail_oid", detail_oid);
			map_order_item.put("item_id", item_id);
			map_order_item.put("sku_id", sku_id);
			map_order_item.put("order_num", String.valueOf(order_num));//商品数量，下单的时的商品数量
			map_order_item.put("actual_num", map.get("actual_num"));//真实数量，后台改单之后的数量，没改之前和order_num一致
			map_order_item.put("cost_price", map.get("cost_price"));//成本价：直接到供应商和师傅临采的的采购价就是成本价，如果是入库的成本价是平均成本价
			map_order_item.put("sum_cost", map.get("sum_cost"));//总成本
			map_order_item.put("uniform_price", map_item.get("uniform_price").toString());//统一售价
			map_order_item.put("customer_price", uniform_price);//客户售价
			map_order_item.put("pay_price", uniform_price);//售价，如果有客户售价则是客户售价否则是标准售价
			map_order_item.put("money", money);//金额
			map_order_item.put("customer_note", customer_note);//客户备注
			map_order_item.put("last_update_note", customer_note);//用来累计备注用的
			map_order_item.put("item_name", map_item.get("item_name").toString());
			if (map_item.get("item_small_img") != null) {
				map_order_item.put("item_small_img", map_item.get("item_small_img").toString());
			}
			map_order_item.put("unit", map_item.get("unit").toString());
			map_order_item.put("val_names", val_names);
			map_order_item.put("source_type", map.get("source_type"));
			if (Integer.valueOf(map.get("source_type")) == 1) {
				map_order_item.put("source_id", "0");
			} else {
				map_order_item.put("source_id", map.get("source_id"));
			}
			map_order_item.put("oi_type", String.valueOf(OiTypeEnum.single.getId()));
			sqlMapper.insert_or_update("p_order_item", map_order_item, null);
		}
		
		//新增记录
		String record= "增加菜品【"+map_item.get("item_name").toString()+"】 规格【"+val_names+"】 数量 "+order_num+"";
		
		String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s, update_type = %s,ref_id = %s ,description = '%s'";
		sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.virtual.getId(),UpdateTypeEnum.update.getId(),oid,record));
		
		//更新订单 备份订单 (虚拟订单)
		update_copy_virtual(member_id,oid);
		
		//更新订单差异
		update_order_diff(oid);
		
		results.setMessage("新增成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:订单列表==>虚拟订单==>修改订单查询
	//{"mid":"1","action":"b_order_virtual_update_get","oid":"3"}
	public void b_order_virtual_update_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int oid = obj_vars.optInt("oid");//订单id
		
		String sqldetail = " select po.oid,  DATE_FORMAT(po.send_time,'%Y-%m-%d %H:%i:%m') "
				+ " send_time, po.diff_amount , po.charge_amount, (po.charge_amount+po.diff_amount) diff_charge from p_order po  where po.oid = "+oid+" ";
		Map<String, Object> mapdetail = sqlMapper.public_one_detail(sqldetail);
		
		//是否可以冲账 1:可以冲账 2:不可以冲账
		String is_charge = ""; 
		//差额为正或负数 1:正 2:负
		String plus_minus = "";
		//冲账信息
		BigDecimal diff_charge = new BigDecimal(mapdetail.get("diff_charge").toString());
		if (diff_charge.compareTo(new BigDecimal(0)) == 1) {
			is_charge = "1";
			plus_minus = "1";
		} else if(diff_charge.compareTo(new BigDecimal(0)) == -1) {
			is_charge = "1";
			plus_minus = "2";
		} else {
			is_charge = "2";
		}
		
		
		Map<String, Object> map = new HashMap<>();
		map.put("mapdetail", mapdetail);
		map.put("is_charge", is_charge);
		map.put("plus_minus", plus_minus);
		
		results.setResult(map);//输出:订单信息 list_real 订单信息  is_charge 1:可以冲账 2:不可以冲账 plus_minus 差额为正或负数 1:正 2:负  diff_charge 用这个值判断大小
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:订单列表==>虚拟订单==>修改订单
	//{"mid":"1","action":"b_order_virtual_update_set","oid":"1","send_time":"2018-05-09 12:12:30","list_order_item":[{"order_item_id":"1","actual_num":"10"},{"order_item_id":"2","actual_num":"20"}],"list_charge":[{"oid":"2","charge_amount":"-8"}]}
	public void b_order_virtual_update_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int oid = obj_vars.optInt("oid");//订单id
		String send_time = obj_vars.optString("send_time");//配送时间
		String list_order_item_str = obj_vars.optString("list_order_item");//订单数量集合 list_order_item [{"order_item_id":"","actual_num":""},...]provide_num 提供数量
		String list_charge_str = obj_vars.optString("list_charge");//冲账集合  list_charge [{"oid":"","diff_charge_amount":""},...]
		
		//判断是否有过备份(如果没有就备份) 真实订单
		if_first_real(oid);
		
		Gson gson = new Gson();
		List<Map<String, String>> list_charge = gson.fromJson(list_charge_str, new TypeToken<List<Map<String, String>>>() {}.getType());
		//判断冲账金额是否相符
		
		//获取订单信息
		String sqldetail = " select po.oid,  DATE_FORMAT(po.send_time,'%Y-%m-%d %H:%i:%m') "
				+ " send_time, po.diff_amount , po.charge_amount, (po.charge_amount+po.diff_amount) diff_charge from p_order po  where po.oid = "+oid+" ";
		Map<String, Object> mapdetail = sqlMapper.public_one_detail(sqldetail);
		
		//冲账相关
		
		BigDecimal diff_charge_sum = new BigDecimal(0);
		
		//p_order_charge
		for (Map<String, String> map_charge : list_charge) {
			diff_charge_sum = diff_charge_sum.add(new BigDecimal(map_charge.get("diff_charge_amount")));
			Map<String, String> map = new HashMap<>();
			map.put("oid", String.valueOf(oid));
			map.put("rel_oid", map_charge.get("oid"));
			map.put("amount", map_charge.get("diff_charge_amount"));
			
			sqlMapper.insert_or_update("p_order_charge", map, null);
			
			//作为关联点单冲账
			String sql = "update p_order set rel_charge_amount = rel_charge_amount+"+Integer.valueOf(map_charge.get("diff_charge_amount"))+" "
					+ ", charge_amount = charge_amount+"+Integer.valueOf(map_charge.get("diff_charge_amount"))+" where oid = "+oid+"";
			sqlMapper.update(sql);
			
		}
		//插入金额
		diff_charge_sum = diff_charge_sum.add(new BigDecimal(mapdetail.get("charge_amount").toString()));
				
		//修改记录
		String record= "";
		
		//修改商品信息数量信息 p_order_item
		List<Map<String, String>> list_order_item = gson.fromJson(list_order_item_str, new TypeToken<List<Map<String, String>>>() {}.getType());
		for (Map<String, String> map : list_order_item) {
			//获取商品名称
			String sql_item_name_actual_num = "select (select item_name from p_item where item_id = poi.item_id) item_name, poi.actual_num "
					+ "from p_order_item  poi where poi.order_item_id = %s";
			
			Map<String, Object> map_record = sqlMapper.public_one_detail(String.format(sql_item_name_actual_num, map.get("order_item_id").toString()));
			record+= "修改菜品【"+map_record.get("item_name")+"】数量   修改前【"+map_record.get("actual_num")+"】 修改后【"+map.get("actual_num")+"】   ";
			
			//更新
			Integer actual_num = Integer.valueOf(map.get("actual_num").toString());
			Integer order_item_id = Integer.valueOf(map.get("order_item_id").toString());
			String sql_update_order_item = " update p_order_item set actual_num = %s ,sum_cost = (cost_price * %s) where order_item_id = %s ";
			sqlMapper.update(String.format(sql_update_order_item, actual_num,actual_num,order_item_id));
		}
		//插入修改记录
		String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s,update_type = %s,ref_id = %s ,description = '%s'";
		sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.virtual.getId(),UpdateTypeEnum.update.getId(),oid,record));
		
		//更新订单 备份订单 (实际订单)
		update_copy_real(member_id, oid);
		
		//更新订单信息
		String sql_update_order = "update p_order set  send_time = '%s', charge_amount = %s  where oid = %s ";
		sqlMapper.update(String.format(sql_update_order,send_time,diff_charge_sum,oid));
		
		//更新订单差异
		update_order_diff(oid);
		
		results.setMessage("修改成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:订单管理==>客户对账单
	//{"mid":"1","action":"b_customer_balance_get","status":"1","order_no":"","start_time":"2018-9-20","end_time":"2018-10-10","company":"","member_name":"","diff_amount":"1","pagenum":"0","pagesize":"0"}
	public void b_customer_balance_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String status = obj_vars.optString("status"); //订单状态 (1:待收货2:已完成3:全部)
		String order_no = obj_vars.optString("order_no");//订单编号
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String salesman_name = obj_vars.optString("salesman_name");//业务员名称
		String deliver_name = obj_vars.optString("deliver_name");//配送员名称
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT po.cid, po.oid, DATE_FORMAT(po.create_time,'%Y-%m-%d') create_time, po.company kehu_company,"
				+ "  hm.member_name salesman_name, hmd.member_name deliver_name"
				+ " , po.order_no, po.status, "
				+ "  ifnull(po.real_pay_amount,0) real_pay_amount, "
				+ "  ifnull(po.virtual_pay_amount,0) virtual_pay_amount, "
				+ "  ifnull(po.diff_amount,0) diff_amount, "
				+ " (case po.jiesuan_status when 1 then '未结算' when 2 then '已结算' end) jiesuan_status, "
				+ "  if((po.status='已完成' and po.jiesuan_status = 1) ,1,2) is_jiesuan   "
				+ "  FROM p_order po  "
				+ " left join p_customer pc on po.cid = pc.cid"
				+ " left join hx_member  hm on pc.salesman_id = hm.member_id "
				+ " left join hx_member hmd on pc.deliver_id = hmd.member_id "
				+ " WHERE 1=1 and po.is_history = 0 ";
		if (status == null || status.equals("")) {
			status = "3";
		}
		if (!StringUtil.isEmpty(status)) {
			switch (status) {
			case "1":
				sql+= " and po.status = '待收货' ";
				break;
			case "2":
				sql+= " and po.status = '已完成' ";
				break;
			case "3":
				sql+= " and (po.status = '已完成' or po.status = '待收货') ";
				break;
			}
		}
		if (!StringUtil.isEmpty(order_no)) {
			sql+= " and po.order_no = '"+order_no+"' ";
		}
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(po.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(po.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and po.company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(salesman_name)) {
			sql+= " and hm.member_name like '%"+salesman_name+"%' ";
		}
		if (!StringUtil.isEmpty(deliver_name)) {
			sql+= " and hmd.member_name like '%"+deliver_name+"%' ";
		}
		sql+= " order by po.create_time desc  ";
		
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		BigDecimal real_sum = new BigDecimal(0);
		BigDecimal virtual_sum = new BigDecimal(0);
		BigDecimal diff = new BigDecimal(0);
		for (Map<String, Object> map : pageModel.mapRlt) {
			real_sum = real_sum.add(new BigDecimal(map.get("real_pay_amount").toString()));
			virtual_sum = virtual_sum.add(new BigDecimal(map.get("virtual_pay_amount").toString()));
		}
		
		diff = real_sum.subtract(virtual_sum);
		Map<String, Object> map = new HashMap<>();
		map.put("real_sum", real_sum);
		map.put("virtual_sum", virtual_sum);
		map.put("diff", diff);
		
		pageModel.data = map;
		
		results.setResult(pageModel);//输出:订单信息 real_sum 实际总计  virtual_sum 虚拟总计  diff 差额
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:订单管理==>客户对账单==>确认结算
	//{"mid":"1","action":"b_customer_balance_set","list":["1","2"]}
	public void b_customer_balance_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String liststr = obj_vars.optString("list");//list["","",""....]
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(liststr, new TypeToken<List<String>>() {}.getType());
		for (String str : list) {
			String sql = "update p_order set jiesuan_status = 2,jiesuan_member_id = %s,jiesuan_time = now() where oid = %s";
			sqlMapper.update(String.format(sql, member_id,Integer.valueOf(str)));
		}
		
		results.setMessage("结算成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:库存管理==>入库管理==>入库明细列表
	//{"mid":"1","action":"b_entry_detail_list_get","pagenum":"0","pagesize":"0"}
	public void b_entry_detail_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String entry_no = obj_vars.optString("entry_no");//入库单号
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//子类id
		String item_name = obj_vars.optString("item_name");//商品名称
		String company = obj_vars.optString("company");//供应商名称(可空)
		String approve_status = obj_vars.optString("approve_status");//审核状态：1未审核；2审核通过；3审核拒绝
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = " SELECT hed.entry_detail_id,he.entry_id, he.entry_no, entry_date, ps.company, "
				+ " (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pa.pid) catalog_parent_name, "
				+ " pa.catalog_name, pi.item_name, psku.val_names, pi.unit, hed.purchase_num, hed.purchase_price, hed.purchase_amount,"
				+ " (case he.approve_status when 1 then '未审核' when 2 then '审核通过' when 3 then '审核拒绝' end) approve_status "
				+ "  FROM hx_entry_detail hed "
				+ " left join hx_entry he on he.entry_id = hed.entry_id "
				+ " left join p_supply ps on he.sid = ps.sid "
				+ " left join p_item pi on hed.item_id = pi.item_id "
				+ " left join p_catalog pa on pi.catalog_id = pa.catalog_id "
				+ " left join p_sku psku on hed.sku_id = psku.sku_id "
				+ " WHERE 1=1   ";
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and he.entry_date >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and he.entry_date <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(entry_no)) {
			sql+= " and he.entry_no = "+entry_no+" ";
		}
		if (!StringUtil.isEmpty(approve_status)) {
			sql+= " and he.approve_status = "+approve_status+" ";
		}
		if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id in (select catalog_id from p_catalog where pid = "+catalog_parent_id+")  ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id =  "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and pi.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(company)) {
			sql+= " and ps.company like '%"+company+"%' ";
		}
		sql+= " order by hed.create_time desc  ";
	
		System.out.println(sql);
		
        PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
        
        BigDecimal purchase_amounts = new BigDecimal(0);
		for (Map<String, Object> map : pageModel.mapRlt) {
			purchase_amounts = purchase_amounts.add(new BigDecimal(map.get("purchase_amount").toString()));
		}
        Map<String, Object> map = new HashMap<>();
        map.put("purchase_amounts", purchase_amounts);
        
        pageModel.data = map;
        
		results.setResult(pageModel);//输出:商品规格信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:库存管理==>入库管理==>入库明细审核
	//{"mid":"1","action":"b_entry_detail_examine_set","entry_id":"1","approve_status":"2","approve_opinion":"这是最新的入库审核"}
	public void b_entry_detail_examine_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int entry_id = obj_vars.optInt("entry_id");// 入库id
		int approve_status = obj_vars.optInt("approve_status");//审核状态:2审核通过；3审核拒绝
		String approve_opinion = obj_vars.optString("approve_opinion");//审核理由
		
		//更新hx_entry
		String sql = "update hx_entry set approve_status = %s where entry_id = %s ";
		sqlMapper.update(String.format(sql, approve_status,entry_id));
		
		//更新之前的审核
		String sqlupdate = "update hx_approve set is_last = 0 where ref_id = %s";
		sqlMapper.update(String.format(sqlupdate, entry_id));
		//新增审核
		String sqladd = "insert into hx_approve (approve_status, approve_opinion, "
				+ "member_id, approve_type, ref_id, is_last) values (%s,'%s',%s,101,%s,1)";
		sqlMapper.insert(String.format(sqladd, approve_status, approve_opinion, member_id, entry_id));
		
		//如果是成功 进行入库操作
		if (approve_status == 2) {//审核成功
			//查询入库单数据
			String sql_detail = "select hed.item_id, hed.sku_id, ps.val_names,hed.purchase_num,hed.purchase_price,hed.purchase_amount from hx_entry_detail hed "
					+ " left join p_sku ps on ps.sku_id = hed.sku_id where hed.entry_id = %s ";
			List<Map<String, Object>> list_detail = sqlMapper.public_list_all_sql(String.format(sql_detail, entry_id));
			for (Map<String, Object> map_detail : list_detail) {
				
				String item_id = map_detail.get("item_id").toString();
				String sku_id = map_detail.get("sku_id").toString();
				String purchase_num = map_detail.get("purchase_num").toString();
				String purchase_price = map_detail.get("purchase_price").toString();
				String purchase_amount = map_detail.get("purchase_amount").toString();
				
				String val_names = "";
				if (map_detail.get("val_names") != null && !map_detail.equals("")) {
					val_names = map_detail.get("val_names").toString();
				}
				
				//处理库存表
				//查看是否有数据
				String sql_is_have = "select count(1) from hx_inventory where item_id = %s and sku_id = %s";
				String is_have = sqlMapper.public_one_scale(String.format(sql_is_have, item_id,sku_id));
				if (Integer.valueOf(is_have) > 0) {//有数据更新 无数据新增
					//获取id
					String sql_inventory_id = "select inventory_id,should_num,real_num,cost_price from hx_inventory where item_id = %s and sku_id = %s";
					Map<String, Object> map_inventory = sqlMapper.public_one_detail(String.format(sql_inventory_id, item_id, sku_id));
					
					String inventory_id = map_inventory.get("inventory_id").toString();
					String should_num = map_inventory.get("should_num").toString();
					String real_num = map_inventory.get("real_num").toString();
					BigDecimal cost_price = new BigDecimal(map_inventory.get("cost_price").toString());
					BigDecimal cost_amount = cost_price.multiply(new BigDecimal(purchase_num));
					
					String sql_update_hx_inventory = "update hx_inventory set cost_price = (total_amount+%s)/(total_num+%s), "
							+ " total_num = total_num+%s,total_amount = total_num*cost_price, today_purchase_num = today_purchase_num+%s "
							+ " ,today_purchase_amount = today_purchase_num*today_purchase_price, should_num = qichu_num+today_purchase_num-today_order_num, "
							+ " should_price = should_num*should_price where inventory_id = %s  ";
					sqlMapper.update(String.format(sql_update_hx_inventory,cost_amount,purchase_num,
							purchase_num,purchase_num,purchase_num,purchase_num,purchase_num,purchase_num,inventory_id));
					
					//插入hx_inventory_detail
					Map<String, String> map_inventory_detail = new HashMap<>();
					map_inventory_detail.put("inventory_id", inventory_id);
					map_inventory_detail.put("item_id", item_id);
					map_inventory_detail.put("sku_id", sku_id);
					map_inventory_detail.put("val_names", val_names);
					map_inventory_detail.put("change_type", String.valueOf(ChangeTypeEnum.entry.getId()));//改变类型：1入库单入库(entry_id)；2订单销售出库(oid)；3用户盘存填写实际库存后的损耗(member_id)
					map_inventory_detail.put("ref_id", String.valueOf(entry_id));//根据改变的类型确定关联id
					map_inventory_detail.put("change_num", purchase_num);//改变的数量
					map_inventory_detail.put("should_num", should_num);//本次改变后的应数量
					map_inventory_detail.put("real_num", real_num);//本次改变后的实际数量
					map_inventory_detail.put("cost_price", map_inventory.get("cost_price").toString());//本次改变时的成本价
					map_inventory_detail.put("cost_amount", cost_amount.toString());//本次改变时的总价
					
					sqlMapper.insert_or_update("hx_inventory_detail", map_inventory_detail, null);
					
				} else {
					
					Map<String, String> map_hx_inventory = new HashMap<>();
					map_hx_inventory.put("item_id", item_id);
					map_hx_inventory.put("sku_id", sku_id);
					map_hx_inventory.put("val_names", val_names);
					map_hx_inventory.put("cost_price", purchase_price.toString());//当前平均成本价
					map_hx_inventory.put("total_num", purchase_num);//当前总库存
					map_hx_inventory.put("total_amount", purchase_amount.toString()); //当前库存总金额
					map_hx_inventory.put("qichu_price", "0");//期初价格，截止到昨天的成本价，每天凌晨将cost_price更新过来
					map_hx_inventory.put("qichu_num", "0");//期初数量，截止到昨天的数量，每天凌晨将total_num更新过来
					map_hx_inventory.put("qichu_amount", "0");//期初库存，截止到昨天的库存金额，每天凌晨将total_amount更新过来
					map_hx_inventory.put("today_purchase_price", purchase_price.toString());//今天的入库平均价
					map_hx_inventory.put("today_purchase_num", purchase_num.toString());//今天入库的数量
					map_hx_inventory.put("today_purchase_amount", purchase_amount.toString());//今天的入库总金额
					map_hx_inventory.put("today_order_price", "0");//今天出库单价
					map_hx_inventory.put("today_order_num", "0");//今天出库数量
					map_hx_inventory.put("today_order_amount", "0");//今天出库总金额
					map_hx_inventory.put("should_price", purchase_price.toString());//应库存单价
					map_hx_inventory.put("should_num", purchase_num.toString());//应该有的库存，也即应库存
					map_hx_inventory.put("should_amount", purchase_amount.toString());//应库存总额
					map_hx_inventory.put("real_price", purchase_price.toString());//实际价格
					map_hx_inventory.put("real_num", purchase_num.toString());//实际数量
					map_hx_inventory.put("real_amount", purchase_amount.toString());//实际金额
					map_hx_inventory.put("loss_price", "0");
					map_hx_inventory.put("loss_num", "0");//损耗的数量
					map_hx_inventory.put("loss_amount", "0");//损耗总金额
					
					int inventory_id = sqlMapper.insert_or_update("hx_inventory", map_hx_inventory, null);
					
					//插入hx_inventory_detail
					Map<String, String> map_inventory_detail = new HashMap<>();
					map_inventory_detail.put("inventory_id", String.valueOf(inventory_id));
					map_inventory_detail.put("item_id", String.valueOf(item_id));
					map_inventory_detail.put("sku_id", String.valueOf(sku_id));
					map_inventory_detail.put("val_names", val_names);
					map_inventory_detail.put("change_type", String.valueOf(ChangeTypeEnum.entry.getId()));//改变类型：1入库单入库(entry_id)；2订单销售出库(oid)；3用户盘存填写实际库存后的损耗(member_id)
					map_inventory_detail.put("ref_id", String.valueOf(entry_id));//根据改变的类型确定关联id
					map_inventory_detail.put("change_num", String.valueOf(purchase_num));//改变的数量
					map_inventory_detail.put("should_num", purchase_num.toString());//本次改变后的应数量
					map_inventory_detail.put("real_num", purchase_num.toString());//本次改变后的实际数量
					map_inventory_detail.put("cost_price", purchase_price.toString());//本次改变时的成本价
					map_inventory_detail.put("cost_amount", purchase_amount.toString());//本次改变时的总价
					
					sqlMapper.insert_or_update("hx_inventory_detail", map_inventory_detail, null);
					
				}
			}
			
			
		}
		
		
		results.setMessage("审核成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:库存管理==>入库管理==>入库明细编辑查询
	//{"mid":"1","action":"b_entry_detail_edit_select_get","entry_detail_id":"1"}
	public void b_entry_detail_edit_select_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int entry_detail_id = obj_vars.optInt("entry_detail_id");// 入库明细id
		
		String sql = "select hed.entry_detail_id, (select company from p_supply where sid = he.sid) company, pc.pid catalog_parent_id, "
				+ "  (select catalog_name from p_catalog where catalog_id = pc.pid) catalog_parent_name, pc.catalog_id catalog_id, "
				+ "  pc.catalog_name , pi.item_id, pi.item_name, ps.sku_id, ps.val_names, pi.unit, hed.purchase_num, hed.purchase_price, hed.purchase_amount "
				+ "  from hx_entry_detail hed "
				+ "  left join hx_entry he on hed.entry_id = he.entry_id"
				+ "  left join p_item pi on pi.item_id = hed.item_id "
				+ "  left join p_catalog pc on pc.catalog_id = pi.catalog_id "
				+ "  left join p_sku ps on hed.sku_id = ps.sku_id "
				+ "  where hed.entry_detail_id = %s ";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql, entry_detail_id));
		
		results.setResult(map);//输出:入库详情信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:库存管理==>入库管理==>获取商品规格
	//{"mid":"1","action":"b_item_sku_get","item_id":"3"}
	public void b_item_sku_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int item_id = obj_vars.optInt("item_id");//商品id
		
		String sql = "select sku_id, val_names, uniform_price from p_sku where item_id = %s and is_delete = 2 order by sku_id;";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sql, item_id));
		
		results.setResult(list);//输出:商品规格信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:库存管理==>入库管理==>入库明细编辑
	public void b_entry_detail_edit_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int entry_detail_id = obj_vars.optInt("entry_detail_id");// 入库明细id
		int item_id = obj_vars.optInt("item_id");//商品id
		int sku_id = obj_vars.optInt("sku_id");//规格id
		int purchase_num = obj_vars.optInt("purchase_num");//数量
		int purchase_price = obj_vars.optInt("purchase_price");//采购价
		int purchase_amount = obj_vars.optInt("purchase_amount");//总价
		
		String sql = "update hx_entry_detail set item_id = %s, sku_id = %s,purchase_num = %s, purchase_price = %s, purchase_amount = %s where entry_detail_id = %s";
		sqlMapper.update(String.format(sql, item_id,sku_id,purchase_num,purchase_price,purchase_amount,entry_detail_id));
		
		results.setMessage("修改成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:库存管理==>获取供应商接口
	//{"mid":"1","action":"b_supply_get","item_id":"3"}
	public void b_supply_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		
		String sql = "select sid supply_id, company from p_supply where supply_type != 2 order by sid";//不是库存供应商
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		results.setResult(list);//输出:供应商信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:库存管理==》入库管理==>入库明细添加获取商品接口
	//{"mid":"1","action":"b_supply_item_name_get","supply_id":"18","catalog_id":"13"}
	public void b_supply_item_name_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int supply_id = obj_vars.optInt("supply_id");//供应商id
		int catalog_id = obj_vars.optInt("catalog_id");//小类id
		
		String sql = "select psi.item_id, (select item_name from p_item where item_id = psi.item_id) item_name, "
				+ " sku_id, (select val_names from p_sku where sku_id = psi.sku_id and is_delete = 2 ) val_names "
				+ "  from p_supply_item  psi where sid = %s and catalog_id = %s order by item_id  ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sql, supply_id, catalog_id));
		
		List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = null;
		List<Map<String,Object>> lstMapitems = null;
		String item_id = "";
		for(Map<String,Object> mapItem: list)
		{
			if(item_id.isEmpty() || !item_id.equals(mapItem.get("item_id").toString()))
			{
				map = new HashMap<String,Object>();
				item_id = mapItem.get("item_id").toString();
				map.put("item_id", mapItem.get("item_id"));
				map.put("item_name", mapItem.get("item_name"));
				//菜单大类	
				listMap.add(map);
				//菜单子类
				lstMapitems = new ArrayList<Map<String,Object>>();
				map.put("items", lstMapitems);
			}
			
			Map<String,Object> mapSkuItem = new HashMap<String,Object>(); 
			mapSkuItem.put("sku_id", mapItem.get("sku_id"));
			mapSkuItem.put("val_names", mapItem.get("val_names"));
			lstMapitems.add(mapSkuItem);
		}
		
		
		results.setResult(listMap);//输出：供应商 list
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:库存管理==>入库管理==>入库明细添加
	public void b_entry_detail_add_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String supply_id = obj_vars.optString("supply_id");//供应商id
		String entry_date = obj_vars.optString("entry_date");//入库时间 2018-08-08
		String entry_no = obj_vars.optString("entry_no");//单号
		String liststr = obj_vars.optString("list");// list:[{"item_id":"","sku_id":"","purchase_num":"","purchase_price":"","purchase_amount":""}]
		
		Gson gson = new Gson();
		List<Map<String, String>> list = gson.fromJson(liststr, new TypeToken<List<Map<String, String>>>() {}.getType());
		BigDecimal purchase_amounts = new BigDecimal(0);
		for (Map<String, String> map : list) {
			purchase_amounts =  purchase_amounts.add(new BigDecimal(map.get("purchase_amount").toString()));
		}
		
		//新增入库p_entry
		Map<String, String> map = new HashMap<>();
		map.put("sid", supply_id);
		map.put("entry_date", entry_date);
		map.put("entry_no", entry_no);
		map.put("member_id", String.valueOf(member_id));
		map.put("purchase_amount", purchase_amounts.toString());
		
		int entry_id = sqlMapper.insert_or_update("hx_entry", map, null);
		
		//新增p_entry_detail
		for (Map<String, String> map_detail : list) {
			
			String item_id = map_detail.get("item_id");
			String sku_id = map_detail.get("sku_id");
			String purchase_num = map_detail.get("purchase_num");
			BigDecimal purchase_price = new BigDecimal(map_detail.get("purchase_price"));
			BigDecimal purchase_amount = new BigDecimal(map_detail.get("purchase_amount"));
			
			String sql_val_names = "select val_names from p_sku where sku_id = %s";
			String val_names = sqlMapper.public_one_scale(String.format(sql_val_names, sku_id));
			
			
			String sql = "insert into hx_entry_detail (entry_id, item_id, sku_id, purchase_num, purchase_price, purchase_amount) values (%s,%s,%s,%s,%s,%s)";
			sqlMapper.insert(String.format(sql, entry_id,item_id,sku_id,purchase_num,purchase_price,purchase_amount));
		}
		
		results.setMessage("添加成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:库存管理==>入库管理==>入库总览
	//{"mid":"1","action":"b_entry_list_get","pagenum":"0","pagesize":"0"}
	public void b_entry_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//子类id
		String item_name = obj_vars.optString("item_name");//商品名称
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = " select hed.entry_detail_id, he.entry_id, he.entry_date, he.entry_no, "
				+ " (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pa.pid) catalog_parent_name, "
				+ " pa.catalog_name, pi.item_name, psku.val_names, pi.unit, hed.purchase_num, hed.purchase_price, hed.purchase_amount "
				+ " from hx_entry_detail hed "
				+ " left join hx_entry he on he.entry_id = hed.entry_id "
				+ " left join p_item pi on hed.item_id = pi.item_id "
				+ " left join p_catalog pa on pi.catalog_id = pa.catalog_id "
				+ " left join p_sku psku on hed.sku_id = psku.sku_id "
				+ " where 1=1 and he.approve_status = 2  "; //审核通过的
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and he.entry_date >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and he.entry_date <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id in (select catalog_id from p_catalog where pid = "+catalog_parent_id+")  ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id =  "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and pi.item_name like '%"+item_name+"%' ";
		}
		sql+= " order by he.entry_id, hed.entry_detail_id  ";
		
		
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		BigDecimal purchase_amounts = new BigDecimal(0);
		for (Map<String, Object> map : pageModel.mapRlt) {
			purchase_amounts = purchase_amounts.add(new BigDecimal(map.get("purchase_amount").toString()));
		}
        Map<String, Object> map = new HashMap<>();
        
        map.put("purchase_amounts", purchase_amounts);
        
        pageModel.data = map;
        
		results.setResult(pageModel);//输出:商品规格信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:出库管理
	//{"mid":"1","action":"b_out_of_list_get","pagenum":"0","pagesize":"0"}
	public void b_out_of_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();//gengeks
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String order_no = obj_vars.optString("order_no");//订单号
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//子类id
		String item_name = obj_vars.optString("item_name");//商品名称
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String company = obj_vars.optString("company");//供应商名称(可空)
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT poi.order_item_id, po.company kehu_company, po.order_no,poi.item_name, "
				+ " DATE_FORMAT(po.send_time,'%Y-%m-%d %H:%i:%m') send_time,"
				+ " (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pa.pid) catalog_parent_name, "
				+ " (case poi.oi_type when 1 then '销售' when 2 then '送货' when 3 then '上单' end) diff_type, "
				+ " pa.catalog_name, poi.val_names, poi.unit, poi.actual_num, poi.pay_price, (poi.actual_num*poi.pay_price) sum_price "
				+ " FROM p_order_item poi "
				+ " inner join p_order_detail pod on poi.detail_oid = pod.detail_oid "
				+ " left join p_order po on po.oid = pod.oid  "
				+ " left join p_item pi on poi.item_id = pi.item_id "
				+ " left join p_catalog pa on pi.catalog_id = pa.catalog_id "
				+ " WHERE 1=1 and pod.order_type = 1 and po.status = '已完成'  and po.is_history = 0 ";
		System.out.println(sql);
		
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(po.send_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(po.send_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(order_no)) {
			sql+= " and po.order_no <= '"+order_no+"' ";
		}
		//if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
		if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id in (select catalog_id from p_catalog where pid = "+catalog_parent_id+")  ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id =  "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and pi.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and po.company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(company)) {
			sql+= " and poi.source_id in (select sid from p_supply where company like '%"+company+"%') ";
		}
		sql+= " order by po.oid, poi.order_item_id ";
		System.out.println(sql);
		
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		Map<String, Object> map = new HashMap<>();
		
		BigDecimal sum_prices = new BigDecimal(0);
		for (Map<String, Object> map_sum_price : pageModel.mapRlt) {
			Object sum_price = map_sum_price.get("sum_price");
			if (sum_price != null && !sum_price.equals("")) {
				sum_prices = sum_prices.add(new BigDecimal(sum_price.toString()));
			}
		}
		
		map.put("sum_prices", sum_prices);
		
		pageModel.data = map;
		
		results.setResult(pageModel);//输出:商品规格信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:库存总览
	//{"mid":"1","action":"b_stock_list_get","date_time":"2018-11-12","pagenum":"0","pagesize":"0"}
	public void b_stock_list_get() throws IOException, SQLException, java.text.ParseException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//子类id
		String item_name = obj_vars.optString("item_name");//商品名称
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String company = obj_vars.optString("company");//供应商名称(可空)
		String date_time = obj_vars.optString("date_time");//时间
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		BigDecimal qichu_sum = new BigDecimal(0); //期初库存总金额
		BigDecimal should_sum = new BigDecimal(0); //应库存总金额
		BigDecimal purchase_sum = new BigDecimal(0); //入库总金额
		BigDecimal order_sum = new BigDecimal(0); //出库总金额
		BigDecimal loss_sum = new BigDecimal(0); //损耗总金额
		 
		//如果是今天的日 就查 hx_inventory 如果是之前的就查 hx_inventory_photo
		
		//判断是否是今天之前
		int i = DateUtils.CompareDate(date_time);
		
		String sql = "select hip.inventory_id, (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pa.pid) catalog_parent_name, "
				+ " pa.catalog_name, pi.item_id, pi.item_name, hip.sku_id, hip.val_names, pi.unit, hip.qichu_num, hip.qichu_price, hip.qichu_amount, "
				+ " hip.should_num, hip.should_price, hip.should_amount, hip.total_num, hip.loss_num, hip.loss_price, hip.loss_amount, "
				+ " (hip.loss_amount/hip.should_amount) loss_rate, hip.today_purchase_amount, hip.today_order_amount ";
		if (i == -1) {//今天以前 查老数据
			sql+= "from hx_inventory_photo hip";
		}
		if (i != -1 || date_time.equals("")) { //不是以前的数据 或者时间为空的时候
			sql+= "from hx_inventory hip";
		}
			sql+= " left join p_item pi on hip.item_id = pi.item_id "
			+ " left join p_catalog pa on pi.catalog_id = pa.catalog_id "
			+ " where 1=1  ";
			
		if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id in (select catalog_id from p_catalog where pid = "+catalog_parent_id+")  ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id =  "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and pi.item_name like '%"+item_name+"%' ";
		}
		if (i == -1 || date_time.equals("")) { //不是以前的数据 或者时间为空的时候
			if (!StringUtil.isEmpty(date_time)) {
				sql+= " and DATE(hip.create_time) = '"+date_time+"' ";
			}
		}
		sql+= " order by hip.inventory_id, hip.sku_id, pi.item_id ";
		
		System.out.println(sql);
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		//计算总金额
		for (Map<String, Object> map_sum : pageModel.mapRlt) {
			String inventory_id = map_sum.get("inventory_id").toString();
			String item_id = map_sum.get("item_id").toString();
			String sku_id = map_sum.get("sku_id").toString();
			
			Object qichu_amount = map_sum.get("qichu_amount");
			Object should_amount = map_sum.get("should_amount");
			Object today_purchase_amount = map_sum.get("today_purchase_amount");
			Object today_order_amount = map_sum.get("today_order_amount");
			Object loss_amount = map_sum.get("loss_amount");
			if (qichu_amount != null && !qichu_amount.equals("")) {
				qichu_sum = qichu_sum.add(new BigDecimal(qichu_amount.toString()));
			}
			if (should_amount != null && !should_amount.equals("")) {
				should_sum = should_sum.add(new BigDecimal(should_amount.toString()));
			}
			if (today_purchase_amount != null && !today_purchase_amount.equals("")) {
				purchase_sum = purchase_sum.add(new BigDecimal(today_purchase_amount.toString()));
			}
			if (today_order_amount != null && !today_order_amount.equals("")) {
				order_sum = order_sum.add(new BigDecimal(today_order_amount.toString()));
			}
			if (loss_amount != null && !loss_amount.equals("")) {
				loss_sum = loss_sum.add(new BigDecimal(loss_amount.toString()));
			}
			
			String sql_enter = "select * from ( select ps.sid, ps.company, hid.real_num, hid.cost_price, hid.cost_amount, hid.create_time "
					+ " from hx_inventory_detail hid "
					+ " inner join hx_entry he on he.entry_id = hid.ref_id "
					+ " inner join p_supply ps on ps.sid = he.sid"
					+ " where hid.inventory_id = "+inventory_id+" and hid.item_id = "+item_id+" and hid.sku_id = "+sku_id+""
					+ " and change_type = "+ChangeTypeEnum.entry.getId()+" order by  hid.inventory_detail_id ) a ";
			if (!StringUtil.isEmpty(company)) {
				sql+= " and a.company like '%"+company+"%' ";
			}
			if (!StringUtil.isEmpty(date_time)) {
				sql+= " and DATE(a.create_time) = '"+date_time+"' ";
			}
			
			List<Map<String, Object>> list_enter = sqlMapper.public_list_all_sql(sql_enter);
			map_sum.put("list_enter", list_enter);//入库数据
			
			String sql_out = " select * from ( select poi.order_item_id, po.company kehu_company, "
					+ " poi.actual_num real_num, poi.pay_price cost_price, (poi.actual_num*poi.pay_price) cost_amount,poi.create_time "
					+ " FROM p_order_item poi "
					+ " inner join p_order_detail pod on poi.detail_oid = pod.detail_oid "
					+ " left join p_order po on po.oid = pod.oid  "
					+ " WHERE 1=1 and pod.order_type = 1  and po.is_history = 0  "
					+ " and poi.item_id = "+item_id+" and poi.sku_id = "+sku_id+" ) a ";
			
			if (!StringUtil.isEmpty(kehu_company)) {
				sql+= " and a.kehu_company like '%"+kehu_company+"%' ";
			}
			if (!StringUtil.isEmpty(date_time)) {
				sql+= " and DATE(a.create_time) = '"+date_time+"' ";
			}
			sql+= " order by a.order_item_id  ";
			List<Map<String, Object>> list_out = sqlMapper.public_list_all_sql(sql_out);
			map_sum.put("list_out", list_out);//出库数据
		}
		Map<String, Object> map = new HashMap<>();
		//统计数据
		Map<String, Object> map_sum = new HashMap<>();
		map_sum.put("qichu_sum", qichu_sum);
		map_sum.put("should_sum", should_sum);
		map_sum.put("purchase_sum", purchase_sum);
		map_sum.put("order_sum", order_sum);
		map_sum.put("loss_sum", loss_sum);
		
		map.put("map_sum", map_sum);
		
		pageModel.data = map;
		
		results.setResult(pageModel);//输出:库存数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	//接口功能:库存管理==>修改实际库存
	public void b_stock_update_total_num_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int inventory_id = obj_vars.optInt("inventory_id");//id
		String total_num = obj_vars.optString("total_num");//数量
		
		String sql = "update hx_inventory set total_num = %s where inventory_id = %s";
		sqlMapper.update(String.format(sql, total_num, inventory_id));
		
		results.setMessage("修改成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:库存总览(备份)
	//{"mid":"1","action":"b_stock_list_get","date_time":"2018-11-12","pagenum":"0","pagesize":"0"}
	public void b_stock_list1_get() throws IOException, SQLException, java.text.ParseException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//子类id
		String item_name = obj_vars.optString("item_name");//商品名称
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String company = obj_vars.optString("company");//供应商名称(可空)
		String date_time = obj_vars.optString("date_time");//时间
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		BigDecimal qichu_sum = new BigDecimal(0); //期初库存总金额
		BigDecimal should_sum = new BigDecimal(0); //应库存总金额
		BigDecimal purchase_sum = new BigDecimal(0); //入库总金额
		BigDecimal order_sum = new BigDecimal(0); //出库总金额
		BigDecimal loss_sum = new BigDecimal(0); //损耗总金额
		 
		//如果是今天的日 就查 hx_inventory 如果是之前的就查 hx_inventory_photo
		
		//判断是否是今天之前
		int i = DateUtils.CompareDate(date_time);
		
		String sql = "select hip.inventory_id, (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pa.pid) catalog_parent_name, "
				+ " pa.catalog_name, pi.item_id, pi.item_name, hip.sku_id, hip.val_names, pi.unit, hip.qichu_num, hip.qichu_price, hip.qichu_amount, "
				+ " hip.should_num, hip.should_price, hip.should_amount, hip.real_num, hip.loss_num, hip.loss_price, hip.loss_amount, "
				+ " (hip.loss_amount/hip.should_amount) loss_rate, hip.today_purchase_amount, hip.today_order_amount ";
		if (i == -1) {//今天以前 查老数据
			sql+= "from hx_inventory_photo hip";
		}
		if (i != -1 || date_time.equals("")) { //不是以前的数据 或者时间为空的时候
			sql+= "from hx_inventory hip";
		}
			sql+= " left join p_item pi on hip.item_id = pi.item_id "
			+ " left join p_catalog pa on pi.catalog_id = pa.catalog_id "
			+ " where 1=1  ";
			
		if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id in (select catalog_id from p_catalog where pid = "+catalog_parent_id+")  ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id =  "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and pi.item_name like '%"+item_name+"%' ";
		}
		if (i == -1 || date_time.equals("")) { //不是以前的数据 或者时间为空的时候
			if (!StringUtil.isEmpty(date_time)) {
				sql+= " and DATE(hip.create_time) = '"+date_time+"' ";
			}
		}
		sql+= " order by hip.inventory_id, hip.sku_id, pi.item_id ";
		
		System.out.println(sql);
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		//计算总金额
		for (Map<String, Object> map_sum : pageModel.mapRlt) {
			Object qichu_amount = map_sum.get("qichu_amount");
			Object should_amount = map_sum.get("should_amount");
			Object today_purchase_amount = map_sum.get("today_purchase_amount");
			Object today_order_amount = map_sum.get("today_order_amount");
			Object loss_amount = map_sum.get("loss_amount");
			if (qichu_amount != null && !qichu_amount.equals("")) {
				qichu_sum = qichu_sum.add(new BigDecimal(qichu_amount.toString()));
			}
			if (should_amount != null && !should_amount.equals("")) {
				should_sum = should_sum.add(new BigDecimal(should_amount.toString()));
			}
			if (today_purchase_amount != null && !today_purchase_amount.equals("")) {
				purchase_sum = purchase_sum.add(new BigDecimal(today_purchase_amount.toString()));
			}
			if (today_order_amount != null && !today_order_amount.equals("")) {
				order_sum = order_sum.add(new BigDecimal(today_order_amount.toString()));
			}
			if (loss_amount != null && !loss_amount.equals("")) {
				loss_sum = loss_sum.add(new BigDecimal(loss_amount.toString()));
			}
			
			//入库 数据
			String sql_enter = " select * from ( select hed.entry_detail_id, ps.company , hed.purchase_num,"
					+ " hed.purchase_price, hed.purchase_amount, hed.create_time from hx_entry_detail hed  "
					+ " inner join hx_entry he on he.entry_id = hed.entry_id "
					+ " left join p_supply ps on he.sid = ps.sid "
					+ " where hed.item_id = "+Integer.valueOf(map_sum.get("item_id").toString())+" "
					+ " and hed.sku_id = "+Integer.valueOf(map_sum.get("sku_id").toString())+" ) a  ";
			if (!StringUtil.isEmpty(company)) {
				sql+= " and a.company like '%"+company+"%' ";
			}
			if (!StringUtil.isEmpty(date_time)) {
				sql+= " and DATE(a.create_time) = '"+date_time+"' ";
			}
			sql+= " order by a.entry_detail_id ";
			List<Map<String, Object>> list_enter = sqlMapper.public_list_all_sql(sql_enter);
			map_sum.put("list_enter", list_enter);//入库数据
			
			String sql_out = " select * from ( select poi.order_item_id, po.company kehu_company, "
					+ " poi.actual_num, poi.pay_price, (poi.actual_num*poi.pay_price) sum_price,poi.create_time "
					+ " FROM p_order_item poi "
					+ " inner join p_order_detail pod on poi.detail_oid = pod.detail_oid "
					+ " left join p_order po on po.oid = pod.oid  "
					+ " WHERE 1=1 and pod.order_type = 1  and po.is_history = 0  "
					+ " and poi.item_id = "+Integer.valueOf(map_sum.get("item_id").toString())+" "
					+ " and poi.sku_id = "+Integer.valueOf(map_sum.get("sku_id").toString())+" ) a ";
			
			if (!StringUtil.isEmpty(kehu_company)) {
				sql+= " and a.kehu_company like '%"+kehu_company+"%' ";
			}
			if (!StringUtil.isEmpty(date_time)) {
				sql+= " and DATE(a.create_time) = '"+date_time+"' ";
			}
			sql+= " order by a.order_item_id  ";
			List<Map<String, Object>> list_out = sqlMapper.public_list_all_sql(sql_out);
			map_sum.put("list_out", list_out);//出库数据
		}
		Map<String, Object> map = new HashMap<>();
		//统计数据
		Map<String, Object> map_sum = new HashMap<>();
		map_sum.put("qichu_sum", qichu_sum);
		map_sum.put("should_sum", should_sum);
		map_sum.put("purchase_sum", purchase_sum);
		map_sum.put("order_sum", order_sum);
		map_sum.put("loss_sum", loss_sum);
		
		map.put("map_sum", map_sum);
		
		pageModel.data = map;
		
		results.setResult(pageModel);//输出:库存数据
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:财务管理
	//{"mid":"1","action":"b_management_get","pagenum":"0","pagesize":"0"}
	public void b_management_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);//gengeks (可以优化)
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String hexiao_status = obj_vars.optString("hexiao_status");//核销状态：1未核销；2已核销;3全部
		String fuhe_status = obj_vars.optString("fuhe_status");//复核状态：1未复核；2已复核;3全部
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String company = obj_vars.optString("company");//供应商名
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//子类id
		String item_name = obj_vars.optString("item_name");//商品名称
		String deliver_name = obj_vars.optString("deliver_name");//配送员名称
		String salesman_name = obj_vars.optString("salesman_name");//业务员名称
		
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT * FROM (select hfsd.fin_supply_detail_id, hfs.fin_supply_no, "
				+ "if(po.jiesuan_time is null,'', DATE_FORMAT(po.jiesuan_time,'%Y-%m-%d %H:%i:%m')) jiesuan_time, hfs.fin_supply_date, "
				+ " DATE_FORMAT(po.create_time,'%Y-%m-%d %H:%i:%m') order_time, po.order_no, "
				+ " pc.company kehu_company, hm.member_name deliver_name, hms.member_name salesman_name ,  pca.pid catalog_parent_id, "
				+ " (select catalog_name from p_catalog pcl where pcl.catalog_id = pca.pid) catalog_parent_name, "
				+ "pca.catalog_id, pca.catalog_name, poi.item_name, poi.val_names, poi.unit, poi.actual_num,  "
				+ " poi.pay_price customer_price, (poi.actual_num* poi.pay_price) money, "
				+ " (case poi.oi_type when 1 then '销售' when 2 then '送货' when 3 then '上单' end) diff_type, "
				+ " poi.customer_note, poi.cost_price, (poi.cost_price*poi.actual_num) sum_cost, "
				+ "(poi.pay_price-poi.cost_price*poi.actual_num) profit, hfsd.hexiao_note , "
				+ " ps.sid, ps.company, (case hfs.fuhe_status when 1 then '未复核' when 2 then '已复核' end)"
				+ " fuhe_status, DATE_FORMAT(hfs.fuhe_time,'%Y-%m-%d %H:%i:%m') fuhe_time, "
				+ " if(hfs.fuhe_member_id is null,'',(select member_name from hx_member where member_id = hfs.fuhe_member_id)) fuhe_member_name "
				+ "  from hx_fin_supply_detail hfsd "
				+ " left join hx_fin_supply hfs on hfs.fin_supply_id = hfsd.fin_supply_id "
				+ " left join p_order po on po.oid = hfsd.oid "
				+ " left join p_customer pc on pc.cid = po.cid"
				+ " left join hx_member hm on hm.member_id = pc.deliver_id"
				+ " left join hx_member hms on hms.member_id = pc.salesman_id "
				+ " left join p_order_item poi on poi.order_item_id = hfsd.order_item_id "
				+ " left join p_item pi on pi.item_id = pi.item_id "
				+ " left join p_catalog pca on pca.catalog_id = pi.catalog_id "
				+ " left join p_supply ps on ps.sid = poi.source_id"
				+ " where poi.source_type = 2 and hfs.is_history = 0 and po.is_history = 0 GROUP BY fin_supply_detail_id ) a "
				+ " WHERE 1=1  ";
		
		if (!StringUtil.isEmpty(hexiao_status)) {
			switch (hexiao_status) {
			case "1":
				sql+= " and a.hexiao_status = '未核销' ";
				break;
			case "2":
				sql+= " and a.hexiao_status = '已核销' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(fuhe_status)) {
			switch (fuhe_status) {
			case "1":
				sql+= " and a.fuhe_status = '未复核' ";
				break;
			case "2":
				sql+= " and a.fuhe_status = '已复核' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(a.hexiao_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(a.hexiao_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and a.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(company)) {
			sql+= " and a.company like '%"+company+"%' ";
		}
		if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
			sql+= " and a.catalog_parent_id = "+catalog_parent_id+" ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and a.catalog_id =  "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and a.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(salesman_name)) {
			sql+= " and a.salesman_name like '%"+salesman_name+"%' ";
		}
		if (!StringUtil.isEmpty(deliver_name)) {
			sql+= " and a.deliver_name like '%"+deliver_name+"%' ";
		}
		System.out.println(sql);
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		BigDecimal sum_money = new BigDecimal(0);//含税总金额
		BigDecimal sum_cost_money = new BigDecimal(0);//采购总金额
		BigDecimal sum_profit = new BigDecimal(0);//总利润
		
		for (Map<String, Object> map_item : pageModel.mapRlt) {
			Object money = map_item.get("money");
			Object sum_cost = map_item.get("sum_cost");
			Object profit = map_item.get("profit");
			if(!(money.equals("") || money == null)) {
				sum_money = sum_money.add(new BigDecimal(money.toString()));
			}
			if(!(sum_cost.equals("") || sum_cost == null)) {
				sum_cost_money = sum_cost_money.add(new BigDecimal(sum_cost.toString()));
			}
			if(!(profit.equals("") || profit == null)) {
				sum_profit = sum_profit.add(new BigDecimal(profit.toString()));
			}
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("sum_money", sum_money);
		map.put("sum_cost_money", sum_cost_money);
		map.put("sum_profit", sum_profit);
		
		pageModel.data = map;
		
		
		results.setResult(pageModel);//输出:是否采购单数据  sum_money:含税总金额 sum_cost_money:采购总金额 sum_profit:总利润
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	
	//接口功能:财务管理==>核销==>师傅采购单
	//{"mid":"1","action":"b_order_item_member_get","pagenum":"0","pagesize":"0"}
	public void b_order_item_member_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);//gengeks (可以优化)
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String hexiao_status = obj_vars.optString("hexiao_status");//核销状态：1未核销；2已核销;3全部
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String order_no = obj_vars.optString("order_no");//订单编号
		String member_name = obj_vars.optString("member_name");//师傅名称
		String item_name = obj_vars.optString("item_name");//商品名称
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		BigDecimal sum_money = new BigDecimal(0);//总金额
		BigDecimal sum_cost_money = new BigDecimal(0);//师傅临采总金额
		BigDecimal sum_profit = new BigDecimal(0);//利润
		
		String sql = " SELECT * FROM ( select poi.order_item_id, po.oid, DATE_FORMAT(po.create_time,'%Y-%m-%d %H:%i:%m')"
				+ " create_time, po.order_no, hm.member_name, po.company kehu_company, "
				+ " (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pca.pid) catalog_parent_name,"
				+ " pca.catalog_name, poi.item_name, poi.val_names, "
				+ " poi.unit, poi.order_num, poi.actual_num, poi.pay_price customer_price, (poi.actual_num*poi.pay_price) money , "
				+ " poi.actual_num actual_num_member, poi.cost_price, (poi.actual_num*poi.cost_price) cost_money, "
				+ " (poi.actual_num*poi.pay_price-poi.actual_num*poi.cost_price) profit , "
				+ " (case poi.oi_type when 1 then '销售' when 2 then '送货' when 3 then '上单' end) diff_type, "
				+ " IF((select COUNT(1) from hx_fin_worker_detail hfwd inner join hx_fin_worker hfw "
				+ "on hfw.fin_worker_id = hfwd.fin_worker_id  where hfwd.oid = po.oid and hfw.is_history = 0) > 0,'已核销','未核销') hexiao_status, "
				+ " IF((select COUNT(1) from hx_fin_worker_detail hfwd inner join hx_fin_worker hfw "
				+ "on hfw.fin_worker_id = hfwd.fin_worker_id  where hfwd.oid = po.oid and hfw.is_history = 0) > 0, "
				+ " (select hfwd.hexiao_note from hx_fin_worker_detail hfwd where hfwd.order_item_id = poi.order_item_id),'') hexiao_note "
				+ " from p_order_item poi "
				+ " inner join p_order_detail pod on poi.detail_oid = pod.detail_oid "
				+ " inner join p_order po on po.oid = pod.oid "
				+ " left join p_item pi on poi.item_id = pi.item_id "
				+ " left join p_catalog pca on pi.catalog_id = pca.catalog_id "
				+ " left join hx_member hm on hm.member_id = poi.source_id "
				+ " where pod.order_type = 1 and poi.source_type = 3 and po.is_history = 0 and po.status = '已完成'  ) a "
				+ " WHERE 1=1  ";//poi.source_type = 3
		
		if (!StringUtil.isEmpty(hexiao_status)) {
			switch (hexiao_status) {
			case "1":
				sql+= " and a.hexiao_status = '未核销' ";
				break;
			case "2":
				sql+= " and a.hexiao_status = '已核销' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(a.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(a.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(order_no)) {
			sql+= " and a.order_no = '"+order_no+"' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and a.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and a.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(member_name)) {
			sql+= " and a.member_name like '%"+member_name+"%' ";
		}
		sql += " order by a.order_item_id ";		
		
		
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		
		for (Map<String, Object> map_item : pageModel.mapRlt) {
			Object money = map_item.get("money");
			Object cost_money = map_item.get("cost_money");
			Object profit = map_item.get("profit");
			if(!(money.equals("") || money == null)) {
				sum_money = sum_money.add(new BigDecimal(map_item.get("money").toString()));
			}
			if(!(cost_money.equals("") || cost_money == null)) {
				sum_cost_money = sum_cost_money.add(new BigDecimal(map_item.get("cost_money").toString()));
			}
			if(!(profit.equals("") || profit == null)) {
				sum_profit = sum_profit.add(new BigDecimal(map_item.get("profit").toString()));
			}
			
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("sum_money", sum_money);
		map.put("sum_cost_money", sum_cost_money);
		map.put("sum_profit", sum_profit);
		
		//核销单修改记录
		List<Map<String, Object>> list_remarks = update_remarks(PrintEnum.worker.getId());
		
		map.put("list_remarks", list_remarks);
		
		pageModel.data = map;
		
		
		results.setResult(pageModel);//输出:是否采购单数据  sum_money:总金额 sum_cost_money:师傅临采总金额 sum_profit:利润
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:财务管理==>核销==>师傅采购单==>核销
	//{"mid":"1","action":"b_order_item_member_hexiao_set","list_order_item":[{"order_item_id":"1","hexiao_note":"很好123","cost_price":"10"},{"order_item_id":"2","hexiao_note":"非常满意123","cost_price":"20"}],"pagenum":"0","pagesize":"0"}
	public void b_order_item_member_hexiao_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_order_item = obj_vars.optString("list_order_item"); //list_order_item [{"order_item_id":"","hexiao_note":"","cost_price":""}]
		
		Map<String, String> map = new HashMap<>();
		map.put("fin_worker_no", this.public_getordercode("FW", member_id));
		map.put("hexiao_member_id", String.valueOf(member_id));
		map.put("hexiao_status", "2");
		map.put("hexiao_time", this.current_time());
		map.put("fuhe_status", "1");
		//修改备注 gengeks
		
		int fin_worker_id = sqlMapper.insert_or_update("hx_fin_worker", map, null);
		
		String record= "";
		
		Gson gson = new Gson();
		List<Map<String, String>> list = gson.fromJson(list_order_item, new TypeToken<List<Map<String, String>>>() {}.getType());
		for (Map<String, String> map_detail : list) {
			int order_item_id = Integer.valueOf(map_detail.get("order_item_id"));
			String hexiao_note = map_detail.get("hexiao_note").toString();
			BigDecimal cost_price = new BigDecimal(map_detail.get("cost_price"));
			
			//order_item_id 重复不核销
			String sql_order_item_id = "select count(1) from hx_fin_worker_detail hfwd "
					+ "inner join hx_fin_worker hfw on hfwd.fin_worker_id = hfw.fin_worker_id "
					+ "  where hfwd.order_item_id = "+order_item_id+" and hfw.is_history = 0 ";
			String count = sqlMapper.public_one_scale(sql_order_item_id);
			if (Integer.valueOf(count) > 0) {
				continue;
			}
			
			//判断价格是否有被修改 1是2否
			if(is_cost_price(order_item_id, cost_price) == 1) {
				String sql_item_name = "select item_name, cost_price from p_order_item where order_item_id = %s";
				Map<String, Object> map_item_cost = sqlMapper.public_one_detail(String.format(sql_item_name, order_item_id));
				
				String sql_update_cost_price = "update p_order_item set cost_price = %s where order_item_id = %s";
				sqlMapper.update(String.format(sql_update_cost_price, cost_price,order_item_id));
				
				record+= "修改菜品【"+map_item_cost.get("item_name")+"】价格  修改前【"+map_item_cost.get("cost_price")+"】   修改后【"+cost_price+"】   ";
			}
			
			String sql = "insert into hx_fin_worker_detail (fin_worker_id, oid, order_item_id, hexiao_note) values (%s,( "
					+ " select pod.oid from p_order_item poi  inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
					+ " where poi.order_item_id =  %s ),%s,'%s') ";
			sqlMapper.insert(String.format(sql, fin_worker_id,order_item_id,order_item_id,hexiao_note));
			
			String sql_oid = "select (select oid from p_order_detail where detail_oid = poi.detail_oid) oid "
					+ "from p_order_item poi where poi.order_item_id = %s";
			int oid = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_oid, order_item_id)));
			
			//更新订单 备份订单 (真实订单)
			update_copy_real(member_id, oid);
		}
		
		//新增记录
		String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s, update_type = %s, ref_id = %s ,description = '%s'";
		sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.worker.getId(),UpdateTypeEnum.defaults.getId(),fin_worker_id,record));
		
		
		results.setMessage("核销成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:财务管理==>核销==>供应商订单
	//{"mid":"1","action":"b_order_item_supply_get","pagenum":"0","pagesize":"0"}
	public void b_order_item_supply_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);//---gengeks 修改记录未返回
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String hexiao_status = obj_vars.optString("hexiao_status");//核销状态：1未核销；2已核销;3全部
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String company = obj_vars.optString("company");//供应商名
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		BigDecimal sum_money = new BigDecimal(0);//总金额
		
		String sql = "SELECT a.supply_id, a.company,a.create_time, a.cost_price,"
				+ " IF ((select count(1) from hx_fin_supply where sid = a.supply_id  and  fin_supply_date = a.create_time) > 0,'已核销','未核销') hexiao_status "
				+ " FROM "
				+ " ( "
				+ " select poi.source_id supply_id,(select company from p_supply where sid = poi.source_id) company, "
				+ " DATE(poi.create_time) create_time, sum(poi.actual_num*poi.cost_price) cost_price "
				+ " from p_order_item poi "
				+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
				+ " inner join p_order po on pod.oid = po.oid  "
				+ " where poi.source_type = 2 and pod.order_type = 1 and po.status = '已完成' group by poi.source_id, DATE(poi.create_time)"
				+ " ) a  ";
		
		if (!StringUtil.isEmpty(hexiao_status)) {
			switch (hexiao_status) {
			case "1":
				sql+= " and a.hexiao_status = '未核销' ";
				break;
			case "2":
				sql+= " and a.hexiao_status = '已核销' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(a.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(a.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(company)) {
			sql+= " and a.company like '%"+company+"%' ";
		}
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		for (Map<String, Object> map_item : pageModel.mapRlt) {
			Object cost_price = map_item.get("cost_price");
			if(!(cost_price.equals("") || cost_price == null) ) {
				sum_money = sum_money.add(new BigDecimal(cost_price.toString()));
			}
		}
		Map<String, Object> map = new HashMap<>();
		map.put("sum_money", sum_money);
		
		//供应商修改记录
		List<Map<String, Object>> list_remarks = update_remarks(PrintEnum.supply.getId());
		
		map.put("list_remarks", list_remarks);
		
		pageModel.data = map;
		
		results.setResult(pageModel);//输出:是否采购单数据  sum_money:总金额 
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:财务管理==>核销==>供应商订单==>外部核销
	public void b_order_supply_hexiao_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String supply_id = obj_vars.optString("supply_id");//供应商id
		String date = obj_vars.optString("date");//日期
		
		//单据日期
		String sql_sum_cost = "select sum(poi.actual_num*poi.cost_price) sum_cost from p_order_item poi "
				+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
				+ " where poi.source_type = 2 and poi.source_id = %s and DATE(poi.create_time) = '%s' and pod.order_type = 1 ";
		String sum_cost = sqlMapper.public_one_scale(String.format(sql_sum_cost, supply_id,date));
		
		Map<String, String> map = new HashMap<>();
		map.put("sid", supply_id);
		map.put("fin_supply_no", this.public_getordercode("FS", member_id));
		map.put("fin_supply_date", date);
		map.put("fin_supply_amount", sum_cost);
		map.put("hexiao_member_id", String.valueOf(member_id));
		map.put("hexiao_status", "2");
		map.put("hexiao_time", this.current_time());
		map.put("fuhe_status", "1");
		//修改备注 gengeks
		
		int fin_supply_id = sqlMapper.insert_or_update("hx_fin_supply", map, null);
		
		//数据复制
		String sql_insert_supply_detail = " insert into hx_fin_supply_detail (fin_supply_id,oid,order_item_id,hexiao_note) "
				+ " select %s, (select oid from p_order_detail where detail_oid = poi.detail_oid) oid, order_item_id, '无' "
				+ " from p_order_item poi where poi.order_item_id in ("
				+ " select order_item_id from p_order_item poi "
				+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
				+ " inner join p_order po on pod.oid = po.oid  "
				+ " where poi.source_type = 2 and poi.source_id = %s and DATE(poi.create_time) = '%s' and po.status = '已完成' and pod.order_type = 1 )";
		System.out.println(String.format(sql_insert_supply_detail, fin_supply_id,supply_id,date));
		sqlMapper.insert(String.format(sql_insert_supply_detail, fin_supply_id,supply_id,date));
		
		//新增记录
		String sql_company = " select company from p_supply where sid = %s ";
		String company = sqlMapper.public_one_scale(String.format(sql_company, supply_id));
		
		String record = "核销了 "+company+ " 在  "+date+" 的订单";
		
		String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s, update_type = %s, ref_id = %s ,description = '%s'";
		sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.supply.getId(),UpdateTypeEnum.defaults.getId(),fin_supply_id,record));
		
		results.setMessage("核销成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:财务管理==>核销==>供应商订单详情==>核销
	//{"mid":"1","action":"b_order_item_supply_hexiao_set","supply_id":"1","oid":"3","list_order_item":[{"order_item_id":"1","hexiao_note":"很好","cost_price":"10"},{"order_item_id":"2","hexiao_note":"非常满意","cost_price":"20"}]}
	public void b_order_item_supply_hexiao_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String supply_id = obj_vars.optString("supply_id");//供应商id
		String date = obj_vars.optString("date");//日期
		String list_order_item = obj_vars.optString("list_order_item"); //list [{"order_item_id":"","cost_price":"","hexiao_note":""}]
		
		//单据日期
		String sql_sum_cost = "select sum(poi.actual_num*poi.cost_price) sum_cost from p_order_item poi "
				+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
				+ " where poi.source_type = 2 and poi.source_id = %s and DATE(poi.create_time) = '%s' and pod.order_type = 1 ";
		String sum_cost = sqlMapper.public_one_scale(String.format(sql_sum_cost, supply_id,date));
		
		Map<String, String> map = new HashMap<>();
		map.put("sid", supply_id);
		map.put("fin_supply_no", this.public_getordercode("FS", member_id));
		map.put("fin_supply_date", date);
		map.put("fin_supply_amount", sum_cost);
		map.put("hexiao_member_id", String.valueOf(member_id));
		map.put("hexiao_status", "2");
		map.put("hexiao_time", this.current_time());
		map.put("fuhe_status", "1");
		//修改备注 gengeks
		
		int fin_supply_id = sqlMapper.insert_or_update("hx_fin_supply", map, null);
		
		Gson gson = new Gson();
		List<Map<String, String>> list = gson.fromJson(list_order_item, new TypeToken<List<Map<String, String>>>() {}.getType());
		for (Map<String, String> map_item : list) {
			Integer order_item_id = Integer.valueOf(map_item.get("order_item_id"));
			String hexiao_note = map_item.get("hexiao_note");
			BigDecimal cost_price = new BigDecimal(map_item.get("cost_price"));
			
			//order_item_id 重复不核销
			String sql_order_item_id = "select count(1) from hx_fin_supply_detail hfsd "
					+ " inner join hx_fin_supply hfs on hfsd.fin_supply_id = hfs.fin_supply_id "
					+ "   where hfsd.order_item_id = "+order_item_id+" and hfs.is_history = 0 ";
			String count = sqlMapper.public_one_scale(sql_order_item_id);
			if (Integer.valueOf(count) > 0) {
				continue;
			}
			
			//判断价格是否有被修改 1是2否
			if(is_cost_price(order_item_id, cost_price) == 1) {
				String sql_item_name = "select item_name, cost_price from p_order_item where order_item_id = %s";
				Map<String, Object> map_item_cost = sqlMapper.public_one_detail(String.format(sql_item_name, order_item_id));
				
				String sql_update_cost_price = "update p_order_item set cost_price = %s where order_item_id = %s";
				sqlMapper.update(String.format(sql_update_cost_price, cost_price,order_item_id));
				
				String res = "修改菜品【"+map_item_cost.get("item_name")+"】价格  修改前【"+map_item_cost.get("cost_price")+"】   修改后【"+cost_price+"】   ";
				
				String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s, update_type = %s, ref_id = %s ,description = '%s'";
				sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.supply_info.getId(),UpdateTypeEnum.defaults.getId(),supply_id,res));
			}
			
			
			String sql = "insert into hx_fin_supply_detail (fin_supply_id, oid, order_item_id, hexiao_note) values"
					+ " (%s,(select oid from p_order_item poi inner join p_order_detail pod on pod.detail_oid = poi.detail_oid where poi.order_item_id = %s),%s,'%s') ";
			System.out.println(String.format(sql,fin_supply_id,order_item_id,order_item_id,hexiao_note));
			sqlMapper.insert(String.format(sql, fin_supply_id,order_item_id,order_item_id,hexiao_note));
			
		}
		
		//获取订单id list
		String sql_oid_list = "select oid from hx_fin_supply_detail where fin_supply_id = %s group by oid";
		List<Map<String, Object>> list_oid = sqlMapper.public_list_all_sql(String.format(sql_oid_list, fin_supply_id));
		
		for (Map<String, Object> map_oid : list_oid) {
			//更新订单 备份订单 (真实订单)
			update_copy_real(member_id, Integer.valueOf(map_oid.get("oid").toString()));
		}
		
		//新增记录
		String sql_company = " select company from p_supply where sid = %s ";
		String company = sqlMapper.public_one_scale(String.format(sql_company, supply_id));
		
		String record = "核销了 "+company+ " 在  "+date+" 的订单";
		
		String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s, update_type = %s, ref_id = %s ,description = '%s'";
		sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.supply.getId(),UpdateTypeEnum.defaults.getId(),fin_supply_id,record));
		
		results.setMessage("核销成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	
	//接口功能:财务管理==>核销==>供应商订单==>核销详情
	//{"mid":"1","action":"b_order_item_supply_hexiao_detail_get","date":"2018-10-25","supply_id":"71"}
	public void b_order_item_supply_hexiao_detail_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int supply_id = obj_vars.optInt("supply_id");//供应商id
		String date = obj_vars.optString("date");//日期
		String hexiao_status = obj_vars.optString("hexiao_status");//核销状态：1未核销；2已核销;3全部
		String order_no = obj_vars.optString("order_no");//订单号
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String item_name = obj_vars.optString("item_name");//商品名称
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		BigDecimal sum_money = new BigDecimal(0);//总金额
		BigDecimal sum_cost_money = new BigDecimal(0);//师傅临采总金额
		BigDecimal sum_profit = new BigDecimal(0);//利润
		
		String sql = "SELECT * FROM (select poi.order_item_id, po.oid, poi.source_id supply_id, DATE(poi.create_time) create_time, po.order_no, ps.company, po.company kehu_company,  "
				+ "  (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pca.pid) catalog_parent_name, pca.catalog_name, poi.item_name, poi.val_names, "
				+ " poi.unit, poi.order_num, poi.actual_num, poi.customer_price, (poi.actual_num*poi.customer_price) money , "
				+ "  poi.cost_price, (poi.actual_num*poi.cost_price) cost_money, "
				+ " (poi.actual_num*poi.customer_price-poi.actual_num*poi.cost_price) profit ,"
				+ " (case poi.oi_type when 1 then '销售' when 2 then '送货' when 3 then '上单' end) diff_type, "
				+ " IF ((select count(1) from hx_fin_supply where sid = poi.source_id  and  fin_supply_date = DATE(poi.create_time)) > 0,'已核销','未核销') hexiao_status,"
				+ " (select hfsd.hexiao_note from hx_fin_supply_detail hfsd where hfsd.order_item_id = poi.order_item_id) hexiao_note "
				+ " from p_order_item poi "
				+ " left join p_order_detail pod on poi.detail_oid = pod.detail_oid "
				+ " left join p_order po on po.oid = pod.oid "
				+ " left join p_item pi on poi.item_id = pi.item_id "
				+ " left join p_catalog pca on pi.catalog_id = pca.catalog_id "
				+ " left join p_supply ps on ps.sid = poi.source_id "
				+ " where pod.order_type = 1 and poi.source_type = 2 and po.status = '已完成' "
				+ " and poi.source_id = "+supply_id+" and DATE(poi.create_time) = '"+date+"' ) a "
				+ " WHERE 1=1 ";
		
		if (!StringUtil.isEmpty(hexiao_status)) {
			switch (hexiao_status) {
			case "1":
				sql+= " and a.hexiao_status = '未核销' ";
				break;
			case "2":
				sql+= " and a.hexiao_status = '已核销' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(order_no)) {
			sql+= " and a.order_no = '"+order_no+"' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and a.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and a.kehu_company like '%"+kehu_company+"%' ";
		}
		sql += " order by a.order_item_id ";
		
        PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
        
		
		for (Map<String, Object> map_item : pageModel.mapRlt) {
			sum_money = sum_money.add(new BigDecimal(map_item.get("money").toString()));
			sum_cost_money = sum_cost_money.add(new BigDecimal(map_item.get("cost_money").toString()));
			sum_profit = sum_profit.add(new BigDecimal(map_item.get("profit").toString()));
			
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("sum_money", sum_money);
		map.put("sum_cost_money", sum_cost_money);
		map.put("sum_profit", sum_profit);
		
		//核销单详情修改记录
		List<Map<String, Object>> list_remarks = update_remarks(PrintEnum.supply_info.getId());
		
		map.put("list_remarks", list_remarks);
		
		pageModel.data = map;
		
		results.setResult(pageModel);//输出:是否采购单数据  sum_money:总金额 sum_cost_money:供应商采购总金额 sum_profit:总利润
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:财务管理==>师傅对账单复核==>师傅对账单
	//{"mid":"1","action":"b_order_item_member_fuhe_get"}
	public void b_order_item_member_fuhe_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String fuhe_status = obj_vars.optString("fuhe_status");//核销状态：1未复核；2已复核;3全部
		String start_time_order = obj_vars.optString("start_time_order");//开始时间 2018-10-10
		String end_time_order = obj_vars.optString("end_time_order");//结束时间  2018-10-10
		String start_time_fuhe = obj_vars.optString("start_time_fuhe");//开始时间 2018-10-10
		String end_time_fuhe = obj_vars.optString("end_time_fuhe");//结束时间  2018-10-10
		String fin_worker_no = obj_vars.optString("fin_worker_no");//结算单流水号
		String order_no = obj_vars.optString("order_no");//订单编号
		String member_name = obj_vars.optString("member_name");//师傅名称
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(修改)
		String item_name = obj_vars.optString("item_name");//商品名称
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		BigDecimal sum_money = new BigDecimal(0);//总金额
		BigDecimal sum_cost_money = new BigDecimal(0);//师傅临采总金额
		BigDecimal sum_profit = new BigDecimal(0);//利润
		
		//先分页
		String sql_limit = " select fin_worker_id, fin_worker_no from hx_fin_worker order by fin_worker_id desc ";
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql_limit, pagenum);
		
		for (Map<String, Object> map : pageModel.mapRlt) {
			//在查数据
			String sql = "select * from (select hfw.fin_worker_id, hfwd.fin_worker_detail_id, hfw.fin_worker_no, "
					+ "  DATE_FORMAT(po.create_time,'%Y-%m-%d %H:%i:%m') order_time, po.order_no, hm.member_name, po.company kehu_company,"
					+ " (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pca.pid) catalog_parent_name, pca.catalog_name, hfwd.order_item_id, "
					+ " poi.item_name, poi.val_names,poi.unit, poi.order_num, poi.actual_num, poi.pay_price customer_price, (poi.actual_num*poi.pay_price) money , "
					+ " poi.cost_price, (poi.actual_num*poi.cost_price) cost_money, (poi.actual_num*poi.pay_price-poi.actual_num*poi.cost_price) profit ,"
					+ " (case poi.oi_type when 1 then '销售' when 2 then '送货' when 3 then '上单' end) diff_type, "
					+ " hfwd.hexiao_note,(case hfw.fuhe_status when 1 then '未复核' when 2 then '已复核' end) fuhe_status, "
					+ " DATE_FORMAT(hfw.fuhe_time,'%Y-%m-%d %H:%i:%m') fuhe_time, IF(hfw.fuhe_member_id is null,'', "
					+ " (select member_name from hx_member where member_id = hfw.fuhe_member_id)) fuhe_name "
					+ "  from hx_fin_worker_detail hfwd "
					+ " left join p_order po on po.oid = hfwd.oid"
					+ " left join p_order_item poi on poi.order_item_id = hfwd.order_item_id"
					+ " left join hx_member hm on poi.source_id = hm.member_id"
					+ " left join p_item pi on poi.item_id = pi.item_id"
					+ " left join p_catalog pca on pi.catalog_id = pca.catalog_id"
					+ " left join hx_fin_worker hfw on hfw.fin_worker_id = hfwd.fin_worker_id "
					+ " where hfw.is_history = 0 and po.is_history = 0  order by hfw.fin_worker_id desc ) b "
					+ " where 1=1 and b.fin_worker_id = "+Integer.valueOf(map.get("fin_worker_id").toString())+" "; //is_history 不是历史订单

			if (!StringUtil.isEmpty(fuhe_status)) {
				switch (fuhe_status) {
				case "1":
					sql+= " and b.fuhe_status = '未复核' ";
					break;
				case "2":
					sql+= " and b.fuhe_status = '已复核' ";
					break;
				case "3":
					break;
				}
			}
			if (!StringUtil.isEmpty(start_time_order)) {
				sql+= " and DATE(b.order_time) >= '"+start_time_order+"' ";
			}
			if (!StringUtil.isEmpty(end_time_order)) {
				sql+= " and DATE(b.order_time) <= '"+end_time_order+"' ";
			}
			if (!StringUtil.isEmpty(start_time_fuhe)) {
				sql+= " and DATE(b.fuhe_time) >= '"+start_time_fuhe+"' ";
			}
			if (!StringUtil.isEmpty(end_time_fuhe)) {
				sql+= " and DATE(b.fuhe_time) <= '"+end_time_fuhe+"' ";
			}
			if (!StringUtil.isEmpty(fin_worker_no)) {
				sql+= " and b.fin_worker_no = '"+fin_worker_no+"' ";
			}
			if (!StringUtil.isEmpty(order_no)) {
				sql+= " and b.order_no = '"+order_no+"' ";
			}
			if (!StringUtil.isEmpty(member_name)) {
				sql+= " and b.member_name like '%"+member_name+"%' ";
			}
			if (!StringUtil.isEmpty(kehu_company)) {
				sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
			}
			if (!StringUtil.isEmpty(item_name)) {
				sql+= " and b.item_name like '%"+item_name+"%' ";
			}
			
			System.out.println(sql);
			List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
			for (Map<String, Object> map_item : list) {
				Object money = map_item.get("money");
				Object cost_money = map_item.get("cost_money");
				Object profit = map_item.get("profit");
				if (!(money.equals("") || money == null) ) {
					sum_money = sum_money.add(new BigDecimal(map_item.get("money").toString()));
				}
				if (!(cost_money.equals("") || cost_money == null) ) {
					sum_cost_money = sum_cost_money.add(new BigDecimal(map_item.get("cost_money").toString()));
				}
				if (!(profit.equals("") || profit == null) ) {
					sum_profit = sum_profit.add(new BigDecimal(map_item.get("profit").toString()));
				}
			}
			map.put("list", list);
			
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("sum_money", sum_money);
		map.put("sum_cost_money", sum_cost_money);
		map.put("sum_profit", sum_profit);
		
		//复核单修改记录
		List<Map<String, Object>> list_remarks = update_remarks(PrintEnum.worker_fuhe.getId());
		
		map.put("list_remarks", list_remarks);
		
		pageModel.data = map;
			
		results.setResult(pageModel);//输出:师傅对账单信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:财务管理==>师傅对账单复核==>师傅复核
	//{"mid":"1","action":"b_order_item_member_fuhe_set","fin_worker_id":"6","list_order_item":[{"order_item_id":"1","cost_price":"100","fuhe_note":"这个太便宜了","fin_worker_detail_id":"3"},{"order_item_id":"2","cost_price":"10000","fuhe_note":"这个太便宜了000","fin_worker_detail_id":"4"}]}
	public void b_order_item_member_fuhe_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_order_item = obj_vars.optString("list_order_item"); //list_order_item [{"order_item_id":"","cost_price":"","fin_worker_detail_id":"","fin_worker_id":""}]
		
		String message = "复核成功";
		int status = 1;
		
		String record = "";
		int fin_worker_ids = 0;
		
		Gson gson = new Gson();
		List<Map<String, String>> list = gson.fromJson(list_order_item, new TypeToken<List<Map<String, String>>>() {}.getType());
		for (Map<String, String> map_item : list) {
			Integer order_item_id = Integer.valueOf(map_item.get("order_item_id"));
			//String fuhe_note = map_item.get("fuhe_note").toString();
			Integer fin_worker_detail_id = Integer.valueOf(map_item.get("fin_worker_detail_id"));
			Integer fin_worker_id = Integer.valueOf(map_item.get("fin_worker_id"));
			BigDecimal cost_price = new BigDecimal(map_item.get("cost_price"));
			
			if(fin_worker_ids == 0){
				fin_worker_ids = fin_worker_id;
			}
			//重复不复核
			String sql_order_item_id = "select count(1) from hx_fin_worker where fin_worker_id = "+fin_worker_id+" and is_history = 0 and fuhe_status = 2";
			String count = sqlMapper.public_one_scale(sql_order_item_id);
			if (Integer.valueOf(count) <= 0) {
				String sql = "update hx_fin_worker set fuhe_member_id = %s , fuhe_status = 2 , fuhe_time = now() where fin_worker_id = %s";
				sqlMapper.update(String.format(sql, member_id,fin_worker_id));
			} 
			
			//判断价格是否有被修改 1是2否
			if(is_cost_price(order_item_id, cost_price) == 1) {
				String sql_item_name = "select item_name, cost_price from p_order_item where order_item_id = %s";
				Map<String, Object> map_item_cost = sqlMapper.public_one_detail(String.format(sql_item_name, order_item_id));
				
				String sql_update_cost_price = "update p_order_item set cost_price = %s where order_item_id = %s";
				sqlMapper.update(String.format(sql_update_cost_price, cost_price,order_item_id));
				
				record+= "修改菜品【"+map_item_cost.get("item_name")+"】价格  修改前【"+map_item_cost.get("cost_price")+"】   修改后【"+cost_price+"】   ";
			}
			
			//修改备注
			/*String sql_update_worker_detail = "update hx_fin_worker_detail set fuhe_note = '%s' where fin_worker_detail_id = %s";
			sqlMapper.update(String.format(sql_update_worker_detail, fuhe_note,fin_worker_detail_id));*/
			
			String sql_oid = "select (select oid from p_order_detail where detail_oid = poi.detail_oid) oid "
					+ "from p_order_item poi where poi.order_item_id = %s";
			int oid = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_oid, order_item_id)));
			
			//更新订单 备份订单 (真实订单)
			update_copy_real(member_id, oid);
		}
		
		//新增记录
		String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s, update_type = %s, ref_id = %s ,description = '%s'";
		sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.worker_fuhe.getId(),UpdateTypeEnum.defaults.getId(),fin_worker_ids,record));
		
		results.setMessage(message);//输出：提示信息
		results.setStatus(status);//输出：状态1成功 0失败
	}
	
	//接口功能:财务管理==>师傅对账单复核==>退回
	//{"mid":"1","action":"b_order_item_member_fuhe_set"}
	public void b_order_item_member_fuhe_tuihui_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int fin_worker_id = obj_vars.optInt("fin_worker_id"); //核销单id
		
		String sql = "update hx_fin_worker set is_history = 1 where fin_worker_id = %s";
		sqlMapper.update(String.format(sql,fin_worker_id));
		
		results.setMessage("退回成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:财务管理==>供应商复核==>供应商订单
	//{"mid":"1","action":"b_order_item_supply_fuhe_get","pagenum":"0","pagesize":"0"}
	public void b_order_item_supply_fuhe_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String fuhe_status = obj_vars.optString("fuhe_status");//复核状态：1未复核；2已复核;3全部
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String fin_supply_no = obj_vars.optString("fin_supply_no");//结算单流水号
		String company = obj_vars.optString("company");//供应商名
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		
		String sql = " SELECT * FROM ( select hfs.fin_supply_id,hfs.fin_supply_no, hfs.fin_supply_date, ps.company, hfs.fin_supply_amount, "
				+ " (case hfs.fuhe_status when 1 then '未复核' when 2 then '已复核' end) fuhe_status, "
				+ " IF(hfs.fuhe_member_id is null, '', (select member_name from hx_member where member_id = hfs.fuhe_member_id)) fuhe_name, "
				+ " DATE_FORMAT(hfs.fuhe_time,'%Y-%m-%d %H:%i:%m') fuhe_time  from hx_fin_supply hfs "
				+ " left join p_supply ps on ps.sid = hfs.sid "
				+ " where hfs.is_history = 0 order by hfs.fin_supply_id desc) b"
				+ " WHERE 1=1 ";
		
		if (!StringUtil.isEmpty(fuhe_status)) {
			switch (fuhe_status) {
			case "1":
				sql+= " and b.fuhe_status = '未复核' ";
				break;
			case "2":
				sql+= " and b.fuhe_status = '已复核' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(b.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(b.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(fin_supply_no)) {
			sql+= " and b.fin_supply_no >= '"+fin_supply_no+"' ";
		}
		if (!StringUtil.isEmpty(company)) {
			sql+= " and b.company like '%"+company+"%' ";
		}
		
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		BigDecimal fin_supply_amounts = new BigDecimal(0);//采购总金额
		for (Map<String, Object> map_item : pageModel.mapRlt) {
			fin_supply_amounts = fin_supply_amounts.add(new BigDecimal(map_item.get("fin_supply_amount").toString()));
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("fin_supply_amounts", fin_supply_amounts);
		
		//核销单修改记录
		List<Map<String, Object>> list_remarks = update_remarks(PrintEnum.supply_fuhe.getId());
		
		map.put("list_remarks", list_remarks);
		
		pageModel.data = map;
		
		results.setResult(pageModel);//输出:供应商订单信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:财务管理==>供应商复核==>复核==>外部复核
	public void b_order_supply_fuhe_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int fin_supply_id = obj_vars.optInt("fin_supply_id");//核销单id
		
		//修改复核状态
		String sql = "update hx_fin_supply set fuhe_member_id = %s, fuhe_status = 2, fuhe_time = now() where fin_supply_id = %s ";
		sqlMapper.update(String.format(sql, member_id, fin_supply_id));
		
		//新增记录
		String sql_company_supply_date = " select (select company from p_supply where sid = hfs.sid) company, fin_supply_date from hx_fin_supply hfs where hfs.fin_supply_id = %s ";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql_company_supply_date, fin_supply_id));
		
		String record = "复核了 "+map.get("company")+ " 在  "+map.get("fin_supply_date")+" 的订单";
		String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s, update_type = %s, ref_id = %s ,description = '%s'";
		sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.supply_fuhe.getId(),UpdateTypeEnum.defaults.getId(),fin_supply_id,record));
		
		results.setMessage("复核成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:财务管理==>供应商复核==>复核
	public void b_order_item_supply_fuhe_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int fin_supply_id = obj_vars.optInt("fin_supply_id");//核销单id
		String list_order_item = obj_vars.optString("list_order_item"); //list_order_item [{"cost_price":"","fin_supply_detail_id":""}]
		
		Gson gson = new Gson();
		List<Map<String, String>> list = gson.fromJson(list_order_item, new TypeToken<List<Map<String, String>>>() {}.getType());
		for (Map<String, String> map_item : list) {
			Integer fin_supply_detail_id = Integer.valueOf(map_item.get("fin_supply_detail_id"));
			//String fuhe_note = map_item.get("fuhe_note").toString();
			BigDecimal cost_price = new BigDecimal(map_item.get("cost_price"));
			
			String sql_order_item_id = "select order_item_id from hx_fin_supply_detail where fin_supply_detail_id = %s";
			int order_item_id = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_order_item_id, fin_supply_detail_id)));
			
			//修改复核状态
			String sql = "update hx_fin_supply set fuhe_member_id = %s, fuhe_status = 2, fuhe_time = now() where fin_supply_id = %s ";
			sqlMapper.update(String.format(sql, member_id, fin_supply_id));
			
			//判断价格是否有被修改 1是2否
			if(is_cost_price(order_item_id, cost_price) == 1) {
				String sql_item_name = "select item_name, cost_price from p_order_item where order_item_id = %s";
				Map<String, Object> map_item_cost = sqlMapper.public_one_detail(String.format(sql_item_name, order_item_id));
				
				String sql_update_cost_price = "update p_order_item set cost_price = %s where order_item_id = %s";
				sqlMapper.update(String.format(sql_update_cost_price, cost_price,order_item_id));
				
				String res = "修改菜品【"+map_item_cost.get("item_name")+"】价格  修改前【"+map_item_cost.get("cost_price")+"】   修改后【"+cost_price+"】   ";
				
				String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s, update_type = %s, ref_id = %s ,description = '%s'";
				sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.supply_fuhe_info.getId(),UpdateTypeEnum.defaults.getId(),fin_supply_detail_id,res));
			}
			
			//修改备注
			/*String sql_update_supply_detail = "update hx_fin_supply_detail set fuhe_note = '%s' where fin_supply_detail_id = %s";
			sqlMapper.update(String.format(sql_update_supply_detail, fuhe_note,fin_supply_detail_id));*/
			
			//根据 fin_supply_detail_id 获取oid
			String sql_oid = "select oid from hx_fin_supply_detail where fin_supply_detail_id = %s";
			int oid = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_oid, fin_supply_detail_id)));
			
		}
		
		//获取订单id list
		String sql_oid_list = "select oid from hx_fin_supply_detail where fin_supply_id = %s group by oid";
		List<Map<String, Object>> list_oid = sqlMapper.public_list_all_sql(String.format(sql_oid_list, fin_supply_id));
		
		for (Map<String, Object> map_oid : list_oid) {
			//更新订单 备份订单 (真实订单)
			update_copy_real(member_id, Integer.valueOf(map_oid.get("oid").toString()));
		}
		
		//新增记录
		String sql_company_supply_date = " select (select company from p_supply where sid = hfs.sid) company, fin_supply_date from hx_fin_supply hfs where hfs.fin_supply_id = %s ";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql_company_supply_date, fin_supply_id));
		
		String record = "复核了 "+map.get("company")+ " 在  "+map.get("fin_supply_date")+" 的订单";
		String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s, update_type = %s, ref_id = %s ,description = '%s'";
		sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.supply_fuhe.getId(),UpdateTypeEnum.defaults.getId(),fin_supply_id,record));
		
		results.setMessage("复核成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:财务管理==>供应商复核==>退回
	//{"mid":"1","action":"b_order_item_supply_fuhe_tuihui_set"}
	public void b_order_item_supply_fuhe_tuihui_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int fin_supply_id = obj_vars.optInt("fin_supply_id");//复核id
		
		String sql = "update hx_fin_supply set is_history = 1 where fin_supply_id = %s ";
		sqlMapper.update(String.format(sql, fin_supply_id));
		
		results.setMessage("退回成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:财务管理==>供应商复核==>供应商对账单详情
	//{"mid":"1","action":"b_order_item_supply_fuhe_detail_get"}
	public void b_order_item_supply_fuhe_detail_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int fin_supply_id = obj_vars.optInt("fin_supply_id"); //复核单id
		String fuhe_status = obj_vars.optString("fuhe_status");//复核状态：1未复核；2已复核;3全部
		String order_no = obj_vars.optString("order_no");//订单号
		String company = obj_vars.optString("company");//供应商名
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(修改)
		String item_name = obj_vars.optString("item_name");//商品名称
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT * FROM (select hfsd.fin_supply_detail_id, DATE_FORMAT(po.create_time,'%Y-%m-%d %H:%i:%m') order_time, "
				+ " po.order_no, ps.company, "
				+ " (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pca.pid) catalog_parent_name, pca.catalog_name, "
				+ " poi.item_name, poi.val_names,poi.unit,po.company kehu_company, poi.order_num, poi.actual_num, poi.customer_price, "
				+ " (poi.actual_num*poi.customer_price) money ,"
				+ " poi.cost_price, (poi.actual_num*poi.cost_price) cost_money,(poi.actual_num*poi.customer_price-poi.actual_num*poi.cost_price) "
				+ " profit, hfsd.hexiao_note, (case hfs.fuhe_status when 1 then '未复核' when 2 then '已复核' end) fuhe_status,  "
				+ " (case poi.oi_type when 1 then '销售' when 2 then '送货' when 3 then '上单' end) diff_type, "
				+ " DATE_FORMAT(hfs.fuhe_time,'%Y-%m-%d %H:%i:%m') fuhe_time, IF(hfs.fuhe_member_id is null, '', "
				+ " (select member_name from hx_member where member_id = hfs.fuhe_member_id)) fuhe_name "
				+ " from hx_fin_supply_detail hfsd "
				+ " left join p_order po on po.oid = hfsd.oid "
				+ " left join hx_fin_supply hfs on hfs.fin_supply_id = hfsd.fin_supply_id "
				+ " left join p_supply ps on ps.sid = hfs.sid "
				+ " left join p_order_item poi on poi.item_id "
				+ " left join p_item pi on poi.item_id = pi.item_id "
				+ " left join p_catalog pca on pi.catalog_id = pca.catalog_id  "
				+ " where hfs.fin_supply_id = "+fin_supply_id+" "
				+ " GROUP BY hfsd.fin_supply_detail_id  order by hfsd.fin_supply_detail_id desc) a"
				+ " WHERE 1=1    ";
		if (!StringUtil.isEmpty(fuhe_status)) {
			switch (fuhe_status) {
			case "1":
				sql+= " and a.fuhe_status = '未核销' ";
				break;
			case "2":
				sql+= " and a.fuhe_status = '已核销' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(order_no)) {
			sql+= " and a.order_no = '"+order_no+"' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and a.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(company)) {
			sql+= " and a.company like '%"+company+"%' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and a.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and a.item_name like '%"+item_name+"%' ";
		}
		System.out.println(sql);
        PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
        BigDecimal sum_money = new BigDecimal(0);//总金额
		BigDecimal sum_cost_money = new BigDecimal(0);//采购总金额
		BigDecimal sum_profit = new BigDecimal(0);//利润
        
		for (Map<String, Object> map_item : pageModel.mapRlt) {
			sum_money = sum_money.add(new BigDecimal(map_item.get("money").toString()));
			sum_cost_money = sum_cost_money.add(new BigDecimal(map_item.get("cost_money").toString()));
			sum_profit = sum_profit.add(new BigDecimal(map_item.get("profit").toString()));
			
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("sum_money", sum_money);
		map.put("sum_cost_money", sum_cost_money);
		map.put("sum_profit", sum_profit);
		
		//复核单详情修改记录
		List<Map<String, Object>> list_remarks = update_remarks(PrintEnum.supply_fuhe_info.getId());
		
		map.put("list_remarks", list_remarks);
		
		pageModel.data = map;
		
		results.setResult(pageModel);//输出:是否采购单数据  sum_money:总金额 sum_cost_money:采购总金额 sum_profit:总利润
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:历史统计==》订单统计
	//{"mid":"1","action":"b_order_count_get"}
	public void b_order_count_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		
		String sum_customer = ""; //总客户数
		String order_customer = ""; //今日下单客户数
		String order_no_customer = ""; //今日未下单客户数
		
		String sql_sum_customer = "select count(1) from p_customer";
		sum_customer = sqlMapper.public_one_scale(sql_sum_customer);
		
		String sql_order_customer = "select count(1) from (select DISTINCT cid from p_order where is_history = 0 and TO_DAYS(create_time) = TO_DAYS(now())) a ";
		order_customer = sqlMapper.public_one_scale(sql_order_customer);
		
		order_no_customer = String.valueOf(Integer.valueOf(sum_customer) - Integer.valueOf(order_customer));
		
		String sql_order_customer_list = "select DATE_FORMAT(po.create_time,'%Y-%m-%d %H:%i') create_time, "
				+ " (select pc.company from p_customer pc where pc.cid = po.cid ) "
				+ " company from p_order po where po.is_history = 0 and TO_DAYS(po.create_time) = TO_DAYS(now()) order by po.oid ";
		List<Map<String, Object>> order_customer_list = sqlMapper.public_list_all_sql(sql_order_customer_list);
		
		String sql_order_no_customer_list = "select pc.company from p_customer pc where pc.cid not in "
				+ "(select DISTINCT cid from p_order where is_history = 0 and TO_DAYS(create_time) = TO_DAYS(now())) order by pc.cid";
		List<Map<String, Object>> order_no_customer_list = sqlMapper.public_list_all_sql(sql_order_no_customer_list);
		
		Map<String, Object> map = new HashMap<>();
		map.put("sum_customer", sum_customer);
		map.put("order_customer", order_customer);
		map.put("order_no_customer", order_no_customer);
		map.put("order_customer_list", order_customer_list);
		map.put("order_no_customer_list", order_no_customer_list);
		
		results.setResult(map);//输出:订单统计
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:历史统计==》资金统计
	//{"mid":"1","action":"b_money_count_get"}
	public void b_money_count_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		
		BigDecimal today_consume = new BigDecimal(0);//今日消费
		BigDecimal week_consume = new BigDecimal(0);//本周消费
		BigDecimal month_consume = new BigDecimal(0);//本月消费
		BigDecimal all_consume = new BigDecimal(0);//累计消费
		
		String sql_today_consume = "select ifnull(SUM(real_pay_amount),0) today_consume from p_order where is_history = 0 and TO_DAYS(create_time) = TO_DAYS(now())";
		today_consume = today_consume.add(new BigDecimal(sqlMapper.public_one_scale(sql_today_consume)));
		
		String sql_week_consume = "select ifnull(SUM(real_pay_amount),0) today_consume from p_order where is_history = 0 and YEARWEEK(DATE_FORMAT(create_time,'%Y-%m-%d')) = YEARWEEK(now())";
		week_consume = week_consume.add(new BigDecimal(sqlMapper.public_one_scale(sql_week_consume)));
		
		String sql_month_consume = "select ifnull(SUM(real_pay_amount),0) today_consume from p_order where is_history = 0 and DATE_FORMAT( create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )";
		month_consume = month_consume.add(new BigDecimal(sqlMapper.public_one_scale(sql_month_consume)));
		
		String sql_all_consume = "select ifnull(SUM(real_pay_amount),0) today_consume from p_order where is_history = 0 ";
		all_consume = all_consume.add(new BigDecimal(sqlMapper.public_one_scale(sql_all_consume)));
		
		
		String sql_list = "select a.click_date,ifnull(b.count_sum,0) as count_sum "
				+ " from ( SELECT curdate() as click_date union all SELECT date_sub(curdate(), interval 1 day) as click_date "
				+ " union all SELECT date_sub(curdate(), interval 2 day) as click_date union all "
				+ " SELECT date_sub(curdate(), interval 3 day) as click_date  union all "
				+ " SELECT date_sub(curdate(), interval 4 day) as click_date  union all "
				+ " SELECT date_sub(curdate(), interval 5 day) as click_date  union all "
				+ " SELECT date_sub(curdate(), interval 6 day) as click_date ) a left join ( "
				+ " select DATE(create_time) as datetime, SUM(real_pay_amount) as count_sum from p_order"
				+ " where is_history = 0 "
				+ " group by DATE(create_time) ) b on a.click_date = b.datetime  ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql_list);
		
		Map<String, Object> map = new HashMap<>();
		map.put("today_consume", today_consume);
		map.put("week_consume", week_consume);
		map.put("month_consume", month_consume);
		map.put("all_consume", all_consume);
		map.put("list", list);
		
		results.setResult(map);//输出:资金统计
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:历史统计==》利润统计
	//{"mid":"1","action":"b_profit_count_get","start_time":"2018-10-10","end_time":"2018-10-30","company":"","salesman_name":"","pagenum":"","pagesize":""}
	public void b_profit_count_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String salesman_name = obj_vars.optString("salesman_name");//业务员名称
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		BigDecimal sales_volumes = new BigDecimal(0);//总销售额
		BigDecimal sales_costs = new BigDecimal(0);//总成本
		BigDecimal sum_profits = new BigDecimal(0);//总利润
		
		String sql = "SELECT b.cid, b.kehu_company, b.salesman_name, b.sales_volume,  b.sales_cost, (b.sales_volume - b.sales_cost) sum_profit, "
				+ "  ROUND(((b.sales_volume - b.sales_cost)/b.sales_volume*100),2) profit_rate  "
				+ "  FROM (select po.cid, po.company kehu_company, SUM(po.real_pay_amount) sales_volume, hm.member_name salesman_name, "
				+ " (select sum(poi.cost_price*poi.actual_num) from p_order_item poi inner join p_order_detail pod on "
				+ "  pod.detail_oid = poi.detail_oid where pod.oid = po.oid) sales_cost, po.create_time "
				+ " from p_order po"
				+ " left join p_customer pc on pc.cid = po.cid "
				+ " left join hx_member "
				+ " hm on hm.member_id = pc.salesman_id "
				+ " where po.is_history = 0 and po.status = '已完成' "
				+ " group by po.cid order by cid) b "
				+ " WHERE 1=1    ";
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(b.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(b.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(salesman_name)) {
			sql+= " and b.salesman_name like '%"+salesman_name+"%' ";
		}
		
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		for (Map<String, Object> map_item : pageModel.mapRlt) {
			map_item.put("account_period", start_time+"——"+end_time);
			Object sales_volume = map_item.get("sales_volume");
			Object sales_cost = map_item.get("sales_cost");
			Object sum_profit = map_item.get("sum_profit");
			if(!(sales_volume.equals("") || sales_volume == null)) {
				sales_volumes = sales_volumes.add(new BigDecimal(sales_volume.toString()));
			}
			if(!(sales_cost.equals("") || sales_cost == null)) {
				sales_costs = sales_costs.add(new BigDecimal(sales_cost.toString()));
			}
			if(!(sum_profit.equals("") || sum_profit == null)) {
				sum_profits = sum_profits.add(new BigDecimal(sum_profit.toString()));
			}
		}
		
		
		Map<String, Object> map = new HashMap<>();
		map.put("sales_volumes", sales_volumes);
		map.put("sales_costs", sales_costs);
		map.put("sum_profits", sum_profits);
		
		//获取打印记录
		List<Map<String, Object>> print_list = print_get(PrintEnum.profit.getId());
		map.put("print_list", print_list);
		
		pageModel.data = map;
		
		results.setResult(pageModel);//输出:利润统计 sales_volumes:总销售额 sales_costs:总成本 sum_profits:总利润 (当页数据)
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:历史统计==》利润统计==》打印
	//{"mid":"1","action":"b_profit_count_print_get","start_time":"2018-10-10","end_time":"2018-10-30","company":"","salesman_name":"","pagenum":"","pagesize":""}
	public void b_profit_count_print_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String salesman_name = obj_vars.optString("salesman_name");//业务员名称
		
		BigDecimal sales_volumes = new BigDecimal(0);//总销售额
		BigDecimal sales_costs = new BigDecimal(0);//总成本
		BigDecimal sum_profits = new BigDecimal(0);//总利润
		
		String sql = "SELECT b.cid, b.kehu_company, b.salesman_name, b.sales_volume,  b.sales_cost, (b.sales_volume - b.sales_cost) sum_profit, "
				+ "  ROUND(((b.sales_volume - b.sales_cost)/b.sales_volume*100),2) profit_rate  "
				+ "  FROM (select po.cid, po.company kehu_company, SUM(po.real_pay_amount) sales_volume, hm.member_name salesman_name, "
				+ " (select sum(poi.cost_price*poi.actual_num) from p_order_item poi inner join p_order_detail pod on "
				+ "  pod.detail_oid = poi.detail_oid where pod.oid = po.oid) sales_cost, po.create_time "
				+ " from p_order po"
				+ " left join p_customer pc on pc.cid = po.cid "
				+ " left join hx_member "
				+ " hm on hm.member_id = pc.salesman_id "
				+ " where po.is_history = 0 and po.status = '已完成' "
				+ " group by po.cid order by cid) b "
				+ " WHERE 1=1    ";
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(b.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(b.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(salesman_name)) {
			sql+= " and b.salesman_name like '%"+salesman_name+"%' ";
		}
		
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		for (Map<String, Object> map_item : list) {
			map_item.put("account_period", start_time+"——"+end_time);
			Object sales_volume = map_item.get("sales_volume");
			Object sales_cost = map_item.get("sales_cost");
			Object sum_profit = map_item.get("sum_profit");
			if (sales_volume != null && !sales_volume.equals("")) {
				sales_volumes = sales_volumes.add(new BigDecimal(map_item.get("sales_volume").toString()));
			}
			if (sales_cost != null && !sales_cost.equals("")) {
				sales_costs = sales_costs.add(new BigDecimal(map_item.get("sales_cost").toString()));
			}
			if (sum_profit != null && !sum_profit.equals("")) {
				sum_profits = sum_profits.add(new BigDecimal(map_item.get("sum_profit").toString()));
			}
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("sales_volumes", sales_volumes);
		map.put("sales_costs", sales_costs);
		map.put("sum_profits", sum_profits);
		map.put("list", list);
		
		//添加打印记录
		print_set(member_id, PrintEnum.profit.getId(), 0,UpdateTypeEnum.defaults.getId());
		
		results.setResult(map);//输出:利润统计 sales_volumes:总销售额 sales_costs:总成本 sum_profits:总利润 (当页数据)
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:历史统计==》利润统计==>每月利润统计
	//{"mid":"1","action":"b_profit_count_twelve_get","kehu_company":"","is_print":""}
	public void b_profit_count_twelve_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		int is_print = obj_vars.optInt("is_print");//是否打印(1:是2:不是) 查数据就传不是
		
		//查询客户
		String sql_customer = "select cid, company from p_customer ";
		if (!StringUtil.isEmpty(kehu_company)) {
			sql_customer+= " where company like '%"+kehu_company+"%' ";
		}
		sql_customer+= " order by cid ";
		List<Map<String, Object>> customer_list = sqlMapper.public_list_all_sql(sql_customer);
		
		
		String sql = "select b.cid, b.kehu_company, b.salesman_name, ifnull(sum(b.sales_volume),0) sales_volume,  "
				+ " ifnull(sum(b.sales_cost),0) sales_cost, ifnull(sum(b.sales_volume - b.sales_cost),0) sum_profit, "
				+ "  ifnull(ROUND(((b.sales_volume - b.sales_cost)/b.sales_volume*100),2),0) profit_rate  "
				+ ",year(b.create_time) year,month(b.create_time) month "
				+ " from(select po.cid, po.company kehu_company, SUM(po.real_pay_amount) sales_volume, hm.member_name salesman_name, "
				+ " (select sum(poi.cost_price*poi.actual_num) from p_order_item poi inner join p_order_detail pod on "
				+ "  pod.detail_oid = poi.detail_oid where pod.oid = po.oid) sales_cost, po.create_time "
				+ " from p_order po "
				+ " left join p_customer pc on pc.cid = po.cid   "
				+ " left join hx_member hm on hm.member_id = pc.salesman_id "
				+ " where po.is_history = 0 and po.status = '已完成' "
				+ " group by po.cid order by cid) b "
				+ "where year(b.create_time) = (SELECT DATE_FORMAT(NOW(), '%Y')) ";
				
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		sql+=" group by b.cid,year(b.create_time), month(b.create_time) order by month(b.create_time) asc, b.cid asc ";
		
		
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		//数据转换
		for (Map<String, Object> map_customer : customer_list) {
			//主cid
			int customer_cid = Integer.valueOf(map_customer.get("cid").toString());
			
			for (Map<String, Object> map : list) {
				int month_cid = Integer.valueOf(map.get("cid").toString());
				int month = Integer.valueOf(map.get("month").toString());
				if (customer_cid == month_cid) {
					if (month == 1) {
						map_customer.put("sales_volume_1", map.get("sales_volume"));
						map_customer.put("sales_cost_1", map.get("sales_cost"));
						map_customer.put("sum_profit_1", map.get("sum_profit"));
						map_customer.put("profit_rate_1", map.get("profit_rate"));
					}
					if (month == 2) {
						map_customer.put("sales_volume_2", map.get("sales_volume"));
						map_customer.put("sales_cost_2", map.get("sales_cost"));
						map_customer.put("sum_profit_2", map.get("sum_profit"));
						map_customer.put("profit_rate_2", map.get("profit_rate"));
					}
					if (month == 3) {
						map_customer.put("sales_volume_3", map.get("sales_volume"));
						map_customer.put("sales_cost_3", map.get("sales_cost"));
						map_customer.put("sum_profit_3", map.get("sum_profit"));
						map_customer.put("profit_rate_3", map.get("profit_rate"));
					}
					if (month == 4) {
						map_customer.put("sales_volume_4", map.get("sales_volume"));
						map_customer.put("sales_cost_4", map.get("sales_cost"));
						map_customer.put("sum_profit_4", map.get("sum_profit"));
						map_customer.put("profit_rate_4", map.get("profit_rate"));
					}
					if (month == 5) {
						map_customer.put("sales_volume_6", map.get("sales_volume"));
						map_customer.put("sales_cost_6", map.get("sales_cost"));
						map_customer.put("sum_profit_6", map.get("sum_profit"));
						map_customer.put("profit_rate_6", map.get("profit_rate"));
					}
					if (month == 7) {
						map_customer.put("sales_volume_7", map.get("sales_volume"));
						map_customer.put("sales_cost_7", map.get("sales_cost"));
						map_customer.put("sum_profit_7", map.get("sum_profit"));
						map_customer.put("profit_rate_7", map.get("profit_rate"));
					}
					if (month == 8) {
						map_customer.put("sales_volume_8", map.get("sales_volume"));
						map_customer.put("sales_cost_8", map.get("sales_cost"));
						map_customer.put("sum_profit_8", map.get("sum_profit"));
						map_customer.put("profit_rate_8", map.get("profit_rate"));
					}
					if (month == 9) {
						map_customer.put("sales_volume_9", map.get("sales_volume"));
						map_customer.put("sales_cost_9", map.get("sales_cost"));
						map_customer.put("sum_profit_9", map.get("sum_profit"));
						map_customer.put("profit_rate_9", map.get("profit_rate"));
					}
					if (month == 10) {
						map_customer.put("sales_volume_10", map.get("sales_volume"));
						map_customer.put("sales_cost_10", map.get("sales_cost"));
						map_customer.put("sum_profit_10", map.get("sum_profit"));
						map_customer.put("profit_rate_10", map.get("profit_rate"));
					}
					if (month == 11) {
						map_customer.put("sales_volume_11", map.get("sales_volume"));
						map_customer.put("sales_cost_11", map.get("sales_cost"));
						map_customer.put("sum_profit_11", map.get("sum_profit"));
						map_customer.put("profit_rate_11", map.get("profit_rate"));
					}
					if (month == 12) {
						map_customer.put("sales_volume_12", map.get("sales_volume"));
						map_customer.put("sales_cost_12", map.get("sales_cost"));
						map_customer.put("sum_profit_12", map.get("sum_profit"));
						map_customer.put("profit_rate_12", map.get("profit_rate"));
					}
				}
			}
			
		}
		
		Map<String, Object> map = new HashMap<>();
		
		List<Map<String, Object>> print_list = new ArrayList<>();
		
		if (is_print == 1) { //增加打印记录
			print_set(member_id, PrintEnum.profit_month.getId(), 0,UpdateTypeEnum.defaults.getId());
		} else {
			print_list = print_get(PrintEnum.profit_month.getId());
		}
		
		map.put("customer_list", customer_list);
		map.put("print_list", print_list);
		
		results.setResult(map);//输出：利润统计12月
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:差额统计
	//{"mid":"1","action":"b_difference_count_get"}
	public void b_difference_count_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String diff_amount = obj_vars.optString("diff_amount");//差额:1:为0 2:小于0 3:大于0
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		BigDecimal sum_real_pay_amount = new BigDecimal(0);//总实际订单金额
		BigDecimal sum_virtual_pay_amount = new BigDecimal(0);//总虚拟订单金额
		BigDecimal sum_diff_amount = new BigDecimal(0);//总差额
		
		String sql = "SELECT po.oid, DATE(po.create_time) create_time, po.company kehu_company, ifnull(real_pay_amount,0) real_pay_amount, "
				+ "ifnull(virtual_pay_amount,0) virtual_pay_amount, ifnull(diff_amount,0) diff_amount "
				+ "   FROM p_order po  WHERE po.is_history = 0 and diff_amount != 0 and po.status = '已完成' ";
		
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(po.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(po.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and po.company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(diff_amount)) {
			switch (diff_amount) {
			case "1":
				sql+= " and po.diff_amount = 0 ";
				break;
			case "2":
				sql+= " and po.diff_amount < 0 ";
				break;
			case "3":
				sql+= " and po.diff_amount > 0 ";
				break;
			}
		}
		sql+= " order by po.create_time desc ";
		
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		for (Map<String, Object> map_item : pageModel.mapRlt) {
			sum_real_pay_amount = sum_real_pay_amount.add(new BigDecimal(map_item.get("real_pay_amount").toString()));
			sum_virtual_pay_amount = sum_virtual_pay_amount.add(new BigDecimal(map_item.get("virtual_pay_amount").toString()));
			sum_diff_amount = sum_diff_amount.add(new BigDecimal(map_item.get("diff_amount").toString()));
		}
		Map<String, Object> map = new HashMap<>();
		map.put("sum_real_pay_amount", sum_real_pay_amount);
		map.put("sum_virtual_pay_amount", sum_virtual_pay_amount);
		map.put("sum_diff_amount", sum_diff_amount);
		
		pageModel.data = map;
		
		results.setResult(pageModel);//输出:差额统计
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:备货管理==>分拣单
	//{"mid":"1","action":"b_sorting_list_get"}
	public void b_sorting_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String item_name = obj_vars.optString("item_name");//商品名称
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(可空)
		String send_time = obj_vars.optString("send_time");//配送日期 2018-06-09
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT * FROM (select poi.order_item_id, poi.item_name, po.company kehu_company,  "
				+ " poi.val_names,poi.unit,poi.actual_num,poi.customer_note, "
				+ " DATE(po.send_time) send_time  from p_order_item poi "
				+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
				+ " inner join p_order po on po.oid = pod.oid "
				+ " where po.is_history = 0 and  pod.order_type = 1 order by poi.item_id ) b "
				+ " WHERE 1=1 ";
		
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and b.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(send_time)) {
			sql+= " and b.send_time = '"+send_time+"' ";
		}
		
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		//打印记录
        List<Map<String, Object>> print_list = print_get(PrintEnum.sorting.getId());
        
        Map<String, Object> map = new HashMap<>();
        map.put("print_list", print_list);
        map.put("send_time", send_time);
        
        pageModel.data = map;
		results.setResult(pageModel);//输出:分拣单列表
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:备货管理==>分拣单打印(不分页数据)
	//{"mid":"1","action":"b_sorting_list_print_get"}
	public void b_sorting_list_print_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String item_name = obj_vars.optString("item_name");//商品名称
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(可空)
		String send_time = obj_vars.optString("send_time");//配送日期 2018-06-09
		
		String sql = "SELECT * FROM (select poi.order_item_id, poi.item_name, po.company kehu_company,  "
				+ " poi.val_names,poi.unit,poi.actual_num,poi.customer_note, "
				+ " DATE(po.send_time) send_time  from p_order_item poi "
				+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
				+ " inner join p_order po on po.oid = pod.oid "
				+ " where po.is_history = 0 and  pod.order_type = 1 order by poi.item_id ) b "
				+ " WHERE 1=1 ";
		
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and b.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(send_time)) {
			sql+= " and b.send_time = '"+send_time+"' ";
		}
		
		List<Map<String, Object>> list  = sqlMapper.public_list_all_sql(sql);
		
		//打印记录
		print_set(member_id, PrintEnum.sorting.getId(), 0, UpdateTypeEnum.defaults.getId());
		
		results.setResult(list);//输出:分拣单列表
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:备货管理==>供应商计划表
	//{"mid":"1","action":"b_supply_play_list_get"}
	public void b_supply_play_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String company = obj_vars.optString("company");//供应商名称(可空)
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(可空)
		String item_name = obj_vars.optString("item_name");//商品名称
		String send_time = obj_vars.optString("send_time");//配送日期 2018-06-09
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = " SELECT * FROM (select poi.order_item_id, psu.company , po.company kehu_company, poi.item_name, "
				+ " poi.val_names,poi.unit,poi.actual_num,poi.customer_note, DATE(po.send_time) send_time  from p_order_item poi "
				+ " left join p_order_detail pod on pod.detail_oid = poi.detail_oid "
				+ " left join p_order po on po.oid = pod.oid  "
				+ " left join p_supply psu on psu.sid = poi.source_id "
				+ " where po.is_history = 0 and  pod.order_type = 1 and poi.source_type = 2 order by psu.sid ) b WHERE 1=1 ";
		
		if (!StringUtil.isEmpty(company)) {
			sql+= " and b.company like '%"+company+"%' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and b.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(send_time)) {
			sql+= " and b.send_time = '"+send_time+"' ";
		}
		
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		//打印记录
        List<Map<String, Object>> print_list = print_get(PrintEnum.supply_play.getId());
        
        Map<String, Object> map = new HashMap<>();
        map.put("print_list", print_list);
        map.put("send_time", send_time);
        
        pageModel.data = map;
		results.setResult(pageModel);//输出:供应商计划表
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:备货管理==>供应商计划表打印(全部数据)
	//{"mid":"1","action":"b_supply_play_list_print_get"}
	public void b_supply_play_list_print_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String company = obj_vars.optString("company");//供应商名称(可空)
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(可空)
		String item_name = obj_vars.optString("item_name");//商品名称
		String send_time = obj_vars.optString("send_time");//配送日期 2018-06-09
		
		String sql = " SELECT * FROM (select poi.order_item_id, psu.company , po.company kehu_company, poi.item_name, "
				+ " poi.val_names,poi.unit,poi.actual_num,poi.customer_note, DATE(po.send_time) send_time  from p_order_item poi "
				+ " left join p_order_detail pod on pod.detail_oid = poi.detail_oid "
				+ " left join p_order po on po.oid = pod.oid  "
				+ " left join p_supply psu on psu.sid = poi.source_id "
				+ " where po.is_history = 0 and  pod.order_type = 1 and poi.source_type = 2 order by psu.sid ) b WHERE 1=1 ";
		
		if (!StringUtil.isEmpty(company)) {
			sql+= " and b.company like '%"+company+"%' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and b.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(send_time)) {
			sql+= " and b.send_time = '"+send_time+"' ";
		}
		
		List<Map<String, Object>> list  = sqlMapper.public_list_all_sql(sql);
		
		//打印记录
		print_set(member_id, PrintEnum.supply_play.getId(), 0,UpdateTypeEnum.defaults.getId());
		
		results.setResult(list);//输出:供应商计划表
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:备货管理==>差异表
	//{"mid":"1","action":"b_diff_list_get"}
	public void b_diff_list_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(可空)
		String item_name = obj_vars.optString("item_name");//商品名称
		String send_time = obj_vars.optString("send_time");//配送日期 2018-06-09
		String diff_type = obj_vars.optString("diff_type");//销售属性 商品的差异类型：销售，上单，送货(汉字)
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT * FROM (select pod.order_diff_id, po.company kehu_company, pod.item_name, pod.val_names, pod.unit, sum(pod.diff_num) diff_num, "
				+ " (case pod.diff_type when 1 then '销售' when 2 then '送货' when 3 then '上单' end) diff_type, DATE(po.send_time) send_time, pod.customer_note "
				+ "   from p_order_diff pod "
				+ " left join p_order po on po.oid = pod.oid "
				+ " where po.is_history = 0  group by po.cid, pod.item_id, pod.sku_id, diff_type "
				+ " order by po.create_time desc) b WHERE 1=1 ";
		
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and b.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and b.send_time like '%"+send_time+"%' ";
		}
		if (!StringUtil.isEmpty(diff_type)) {
			sql+= " and b.diff_type like '%"+diff_type+"%' ";
		}
		
		PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
		//打印记录
        List<Map<String, Object>> print_list = print_get(PrintEnum.diff.getId());
        
        Map<String, Object> map = new HashMap<>();
        map.put("print_list", print_list);
        
        pageModel.data = map;
		results.setResult(pageModel);//输出:差异表
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:备货管理==>差异表打印(全部数据)
	//{"mid":"1","action":"b_diff_list_print_get"}
	public void b_diff_list_print_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(可空)
		String item_name = obj_vars.optString("item_name");//商品名称
		String send_time = obj_vars.optString("send_time");//配送日期 2018-06-09
		String diff_type = obj_vars.optString("diff_type");//销售属性 商品的差异类型：销售，上单，送货(汉字)
		
		String sql = "SELECT * FROM (select pod.order_diff_id, po.company kehu_company, pod.item_name, pod.val_names, pod.unit, sum(pod.diff_num) diff_num, "
				+ " (case pod.diff_type when 1 then '销售' when 2 then '上单' when 1 then '送货' end) diff_type, DATE(po.send_time) send_time, pod.customer_note  "
				+ "   from p_order_diff pod "
				+ " left join p_order po on po.oid = pod.oid "
				+ " where po.is_history = 0 group by po.cid, pod.item_id, pod.sku_id, diff_type "
				+ " order by po.create_time desc) b WHERE 1=1 ";
		
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and b.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and b.send_time like '%"+send_time+"%' ";
		}
		if (!StringUtil.isEmpty(diff_type)) {
			sql+= " and b.diff_type like '%"+diff_type+"%' ";
		}
		
		List<Map<String, Object>> list  = sqlMapper.public_list_all_sql(sql);
		
		//打印记录
		print_set(member_id, PrintEnum.diff.getId(), 0 ,UpdateTypeEnum.defaults.getId());
		
		results.setResult(list);//输出:差异表
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:备货管理==>采购单
	//{"mid":"1","action":"b_purchase_list_get","date":"2010-11-18"}
	public void b_purchase_list_get() throws IOException, SQLException, java.text.ParseException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String date = obj_vars.optString("date");//时间
		String item_name = obj_vars.optString("item_name");//商品名称
		
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "";
		
		//判断是否是今天之前
		int i = DateUtils.CompareDate(date);
		if (i == -1) { //今天以前 查老数据
			sql += "SELECT * FROM ( select psku.sku_id, pi.item_name, "
					+ " ((select sum(poi.actual_num) from p_order_item poi "
					+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
					+ " inner join p_order po on po.oid = pod.oid  "
					+ " where po.is_history = 0 and poi.sku_id = psku.sku_id and  date(po.send_time) = '"+date+"') - "
					+ " (select hip.total_num from hx_inventory_photo hip where hip.sku_id = psku.sku_id AND DATE(hip.create_time) = '"+date+"' )) subtraction,"
					+ " psku.val_names, pi.unit from p_sku psku  "
					+ " inner join p_item pi on psku.item_id = pi.item_id "
					+ " where psku.is_delete = 2  order by psku.sku_id ) a"
					+ " WHERE a.subtraction is not null and a.subtraction > 0  ";
			if (!StringUtil.isEmpty(item_name)) {
				sql+= " and a.item_name like '%"+item_name+"%' ";
			}
		} else {
			sql += "SELECT * FROM ( select psku.sku_id, pi.item_name, "
					+ " ((select sum(poi.actual_num) from p_order_item poi "
					+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
					+ " inner join p_order po on po.oid = pod.oid  "
					+ " where po.is_history = 0 and poi.sku_id = psku.sku_id and  date(po.send_time) = '"+date+"') - "
					+ " (select hi.total_num from hx_inventory hi where hi.sku_id = psku.sku_id )) subtraction,"
					+ " psku.val_names, pi.unit from p_sku psku  "
					+ " inner join p_item pi on psku.item_id = pi.item_id "
					+ " where psku.is_delete = 2  order by psku.sku_id ) a"
					+ " WHERE a.subtraction is not null and a.subtraction > 0  ";
			if (!StringUtil.isEmpty(item_name)) {
				sql+= " and a.item_name like '%"+item_name+"%' ";
			}
		}
		
		System.out.println(sql);
        PageModel pageModel = sqlMapper.public_list_pagenum_sql(sql, pagenum);
		
        //打印记录
        List<Map<String, Object>> print_list = print_get(PrintEnum.purchase.getId());
        
        Map<String, Object> map = new HashMap<>();
        map.put("print_list", print_list);
        
        pageModel.data = map;
        
		results.setResult(pageModel);//输出:差异表
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
		
	}
	
	//接口功能:备货管理==>采购单打印(全部数据)
	//{"mid":"1","action":"b_purchase_list_print_get","date":"2010-10-20"}
	public void b_purchase_list_print_get() throws IOException, SQLException, java.text.ParseException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String date = obj_vars.optString("date");//时间
		String item_name = obj_vars.optString("item_name");//商品名称
		
		
        String sql = "";
		
		//判断是否是今天之前
		int i = DateUtils.CompareDate(date);
		if (i == -1) { //今天以前 查老数据
			sql += "SELECT * FROM ( select psku.sku_id, pi.item_name, "
					+ " ((select sum(poi.actual_num) from p_order_item poi "
					+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
					+ " inner join p_order po on po.oid = pod.oid  "
					+ " where po.is_history = 0 and poi.sku_id = psku.sku_id and  date(po.send_time) = '"+date+"') - "
					+ " (select hip.total_num from hx_inventory_photo hip where hip.sku_id = psku.sku_id AND DATE(hip.create_time) = '"+date+"' )) subtraction,"
					+ " psku.val_names, pi.unit from p_sku psku  "
					+ " inner join p_item pi on psku.item_id = pi.item_id "
					+ " where psku.is_delete = 2  order by psku.sku_id ) a"
					+ " WHERE a.subtraction is not null and a.subtraction > 0  ";
			if (!StringUtil.isEmpty(item_name)) {
				sql+= " and a.item_name like '%"+item_name+"%' ";
			}
		} else {
			sql += "SELECT * FROM ( select psku.sku_id, pi.item_name, "
					+ " ((select sum(poi.actual_num) from p_order_item poi "
					+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
					+ " inner join p_order po on po.oid = pod.oid  "
					+ " where po.is_history = 0 and poi.sku_id = psku.sku_id and  date(po.send_time) = '"+date+"') - "
					+ " (select hi.total_num from hx_inventory hi where hi.sku_id = psku.sku_id )) subtraction,"
					+ " psku.val_names, pi.unit from p_sku psku  "
					+ " inner join p_item pi on psku.item_id = pi.item_id "
					+ " where psku.is_delete = 2  order by psku.sku_id ) a"
					+ " WHERE a.subtraction is not null and a.subtraction > 0  ";
			if (!StringUtil.isEmpty(item_name)) {
				sql+= " and a.item_name like '%"+item_name+"%' ";
			}
		}
		
		List<Map<String, Object>> list  = sqlMapper.public_list_all_sql(sql);
		
		//打印记录
		print_set(member_id, PrintEnum.purchase.getId(), 0, UpdateTypeEnum.defaults.getId());
		
		results.setResult(list);//输出:差异表
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
		
	}
	
	//接口功能:关于平台==>查询
	public void b_about_platform_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int brand_type = obj_vars.optInt("brand_type");//品牌id
		
		String sql = "select brand_type, brand_img, f_img(brand_img,0) http_brand_img, app_desc, phone from p_brand where brand_type = %s";
		
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql, brand_type));
		
		results.setResult(map);//输出:关于平台信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:关于平台==>保存
	public void b_about_platform_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int brand_id = obj_vars.optInt("brand_id");//品牌id
		String brand_img = obj_vars.optString("brand_img");//logo
		String app_desc = obj_vars.optString("app_desc");//介绍
		String phone = obj_vars.optString("phone");//电话
		
		
		String sql = "update p_brand set brand_img = '%s' , app_desc = '%s', phone = '%s' where brand_type = %s";
		sqlMapper.update(String.format(sql, brand_img,app_desc,phone,brand_id));
		
		results.setMessage("保存成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:管理员设置==>删除
	public void b_admin_delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//管理员id 集合
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			String sql = "update hx_member set is_delete = 1 where member_id = %s ";
			sqlMapper.update(String.format(sql, id));
		}
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:供应商列表==>删除
	public void b_supply_delete_set() throws IOException ,SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//供应商id 集合
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			String sql = "update p_user set is_delete = 1 where uid =  %s ";
			sqlMapper.update(String.format(sql, id));
		}
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:客户列表==>删除
	public void b_customer_delete_set() throws IOException ,SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//客户id 集合
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			String sql_customer_supply = "delete from p_customer_supply where cid = %s";
			sqlMapper.delete(String.format(sql_customer_supply, id));
			
			String sql_customer = "delete from p_customer where cid = %s";
			sqlMapper.delete(String.format(sql_customer, id));
		}
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:下单用户管理==>删除
	public void b_user_delete_set() throws IOException ,SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//下单用户id 集合
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			String sql = "update p_user set is_delete = 1 where uid =  %s ";
			sqlMapper.update(String.format(sql, id));
		}
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:轮播图管理==>删除
	public void b_adv_delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//轮播图id 集合
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			String sql = "delete from p_adv where adv_id = %s";
			sqlMapper.delete(String.format(sql, id));
		}
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:首页栏目管理==>删除
	public void b_catalog_home_delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//首页栏目id 集合
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			String sql = "delete from p_catalog_home where home_catalog_id = %s";
			sqlMapper.delete(String.format(sql, id));
		}
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:消息管理==>删除(系统通知 公告信息都是这个接口)
	public void b_message_define_delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//首页栏目id 集合
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			String sql = "delete from p_message_define where msg_define_id = %s";
			sqlMapper.delete(String.format(sql, id));
		}
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:栏目管理==>大类删除
	public void b_catalog_parent_delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		
		//删除大类的子类
		String sql_son = " delete from p_catalog where pid = %s ";
		sqlMapper.delete(String.format(sql_son, catalog_parent_id));
		//删除大类
		String sql_parent = " delete from p_catalog where catalog_id = %s ";
		sqlMapper.delete(String.format(sql_parent, catalog_parent_id));
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:栏目管理==>子类删除
	public void b_catalog__delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String catalog_id = obj_vars.optString("catalog_id");//大类id
		
		String sql = " delete from p_catalog where catalog_id = %s ";
		sqlMapper.delete(String.format(sql, catalog_id));
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:商品管理==>删除
	public void b_item_delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//商品id 集合
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			String sql = "update p_item set is_delete = 1 where item_id =  %s";
			sqlMapper.update(String.format(sql, id));
		}
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:商品管理==>常用属性设置 删除
	public void b_item_attr_val_delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//常用属性id 集合
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			String sql_attr_val = "delete from p_attr_val where attr_id = %s";
			sqlMapper.delete(String.format(sql_attr_val, id));
			
			String sql_attr = " delete from p_attr where attr_id = %s ";
			sqlMapper.delete(String.format(sql_attr, id));
		}
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:客户列表==>自动派单设置删除
	public void b_customer_supply_delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//自动派单设置id 集合
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			String sql = "delete from p_customer_supply where customer_supply_id = %s";
			sqlMapper.delete(String.format(sql, id));
		}
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:订单列表==>删除
	public void b_order_delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//订单id 集合
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			String sql = "update p_order set is_history = 2 where oid =  %s";
			sqlMapper.update(String.format(sql, id));
		}
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}	
	
	//接口功能:订单列表==>实际订单==>删除
	public void b_order_real_delete() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//实际订单商品id 集合
		
		int detail_oid = 0;
		int oid = 0;
		
		//新增删除记录
		String record= "";
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			if (detail_oid == 0) {
				String sql_detail_oid = "select detail_oid from p_order_item where order_item_id = %s";
				detail_oid = Integer.parseInt(sqlMapper.public_one_scale(String.format(sql_detail_oid, id)));
				//判断是否有过备份(如果没有就备份)
				if_first(detail_oid);
				
				String sql_oid = "select oid from p_order_detail where detail_oid = %s";
				oid = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_oid, detail_oid)));
			}
			
			//获取商品名称
			String sql_item_name_actual_num = "select (select item_name from p_item where item_id = poi.item_id) item_name, poi.actual_num "
					+ " , (select val_names from p_sku where sku_id = poi.sku_id) val_names  "
					+ "from p_order_item  poi where poi.order_item_id = %s";
			Map<String, Object> map_record = sqlMapper.public_one_detail(String.format(sql_item_name_actual_num, id));
			
			record+= "删除菜品【"+map_record.get("item_name")+"】 规格【"+map_record.get("val_names")+"】 数量 "+map_record.get("actual_num")+"  ";
			
			String sql = "delete from p_order_item where order_item_id = %s";
			sqlMapper.delete(String.format(sql, id));
			
		}
		
		//增加删除记录
		String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s,update_type = %s,ref_id = %s ,description = '%s'";
		sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.real.getId(),UpdateTypeEnum.update.getId(),oid,record));
		
		//更新订单 备份订单 (真实订单)
		update_copy_real(member_id, oid);
		
		//订单修改通知
		order_update_notice(oid);
		
		//更新订单差异
		update_order_diff(oid);
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:订单列表==>虚拟订单==>删除
	public void b_order_virtual_delete() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//虚拟订单商品id 集合
		
		int detail_oid = 0;
		int oid = 0;
		
		//新增删除记录
		String record= "";
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			if (detail_oid == 0) {
				String sql_detail_oid = "select detail_oid from p_order_item where order_item_id = %s";
				detail_oid = Integer.parseInt(sqlMapper.public_one_scale(String.format(sql_detail_oid, id)));
				//判断是否有过备份(如果没有就备份)
				if_first(detail_oid);
				
				String sql_oid = "select oid from p_order_detail where detail_oid = %s";
				oid = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_oid, detail_oid)));
			}
			
			//获取商品名称
			String sql_item_name_actual_num = "select (select item_name from p_item where item_id = poi.item_id) item_name, poi.actual_num "
					+ " , (select val_names from p_sku where sku_id = poi.sku_id) val_names  "
					+ "from p_order_item  poi where poi.order_item_id = %s";
			Map<String, Object> map_record = sqlMapper.public_one_detail(String.format(sql_item_name_actual_num, id));
			
			record+= "删除菜品【"+map_record.get("item_name")+"】 规格【"+map_record.get("val_names")+"】 数量 "+map_record.get("actual_num")+"  ";
			
			String sql = "delete from p_order_item where order_item_id = %s";
			sqlMapper.delete(String.format(sql, id));
		}
		
		//增加删除记录
		String sql_insert_hx_print = "insert into hx_print set member_id = %s,print_type = %s,update_type = %s,ref_id = %s ,description = '%s'";
		sqlMapper.insert(String.format(sql_insert_hx_print, member_id,PrintEnum.virtual.getId(),UpdateTypeEnum.update.getId(),oid,record));
		
		//更新订单 备份订单 (虚拟订单)
		update_copy_virtual(member_id, oid);
		
		//更新订单差异
		update_order_diff(oid);
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	
	//接口功能:用户反馈==>删除
	public void b_feedback_delete_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String list_id = obj_vars.optString("list");//用户反馈id 集合
		
		Gson gson = new Gson();
		List<String> list = gson.fromJson(list_id, new TypeToken<List<String>>() {}.getType());
		for (String id : list) {
			String sql = "delete from p_feedback where feedback_id = %s";
			sqlMapper.delete(String.format(sql, id));
		}
		
		results.setMessage("删除成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:导出execl
	//{"mid":"1","action":"b_derive_info_get","export_type":"100"}
	public void b_derive_info_get() throws IOException, SQLException, ClassNotFoundException, IntrospectionException, IllegalAccessException,
			ParseException, InvocationTargetException, UnsupportedEncodingException{
		JSONObject obj_vars =  getObj_vars();
		int export_type = obj_vars.optInt("export_type");//导出类型 100 商品导出
		String export_name = "";
		
	    	if (export_type == ExportEnum.item.getId()) {
	    		export_name = ExportEnum.item.getValue();
			}
	    	if (export_type == ExportEnum.sorting.getId()) {
				export_name = ExportEnum.sorting.getValue();
			}
	    	if (export_type == ExportEnum.supply_play.getId()) {
				export_name = ExportEnum.supply_play.getValue();
			}
	    	if (export_type == ExportEnum.diff.getId()) {
				export_name = ExportEnum.diff.getValue();
			}
	    	if (export_type == ExportEnum.stock.getId()) {
				export_name = ExportEnum.stock.getValue();
			}
	    	if (export_type == ExportEnum.entry.getId()) {
				export_name = ExportEnum.entry.getValue();
			}
	    	if (export_type == ExportEnum.entry_detail.getId()) {
				export_name = ExportEnum.entry_detail.getValue();
			}
	    	if (export_type == ExportEnum.entry_out.getId()) {
				export_name = ExportEnum.entry_out.getValue();
			}
	    	if (export_type == ExportEnum.balance.getId()) {
				export_name = ExportEnum.balance.getValue();
			}
	    	if (export_type == ExportEnum.worker.getId()) {
				export_name = ExportEnum.worker.getValue();
			}
	    	if (export_type == ExportEnum.supply.getId()) {
				export_name = ExportEnum.supply.getValue();
			}
	    	if (export_type == ExportEnum.worker_fuhe.getId()) {
				export_name = ExportEnum.worker_fuhe.getValue();
			}
	    	if (export_type == ExportEnum.supply_fuhe.getId()) {
				export_name = ExportEnum.supply_fuhe.getValue();
			}
	    	if (export_type == ExportEnum.profit.getId()) {
				export_name = ExportEnum.profit.getValue();
			}
	    	if (export_type == ExportEnum.profit_twelve.getId()) {
				export_name = ExportEnum.profit_twelve.getValue();
			}
	    	if (export_type == ExportEnum.bill.getId()) {
				export_name = ExportEnum.bill.getValue();
			}
	    	if (export_type == ExportEnum.management.getId()) {
				export_name = ExportEnum.management.getValue();
			}
	    	export_name+= ".xlsx";
	    	
	        response.reset(); //清除buffer缓存
	        Map<String,Object> map=new HashMap<String,Object>();
	        // 指定下载的文件名，浏览器都会使用本地编码，即GBK，浏览器收到这个文件名后，用ISO-8859-1来解码，然后用GBK来显示
	        // 所以我们用GBK解码，ISO-8859-1来编码，在浏览器那边会反过来执行。
	        response.setHeader("Content-Disposition", "attachment;filename=" + new String(export_name.getBytes("GBK"),"ISO-8859-1"));
	        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
	        response.setHeader("Pragma", "no-cache");
	        response.setHeader("Cache-Control", "no-cache");
	        response.setDateHeader("Expires", 0);
			response.setHeader("Access-Control-Allow-Origin", "*"); 
	        XSSFWorkbook workbook=null;
	        //导出Excel对象
	        if (export_type == ExportEnum.item.getId()) {
	        	workbook = this.exportExcelInfo_item(export_name);
			}
	        if (export_type == ExportEnum.sorting.getId()) {
				workbook = this.exportExcelInfo_sorting(export_name);
			}
	        if (export_type == ExportEnum.supply_play.getId()) {
				workbook = this.exportExcelInfo_supply_play(export_name);
			}
	        if (export_type == ExportEnum.diff.getId()) {
				workbook = this.exportExcelInfo_diff(export_name);
			}
	        if (export_type == ExportEnum.stock.getId()) {
				workbook = this.exportExcelInfo_stock(export_name);
			}
	        if (export_type == ExportEnum.entry.getId()) {
				workbook = this.exportExcelInfo_entry(export_name);
			}
	        if (export_type == ExportEnum.entry_detail.getId()) {
				workbook = this.exportExcelInfo_entry_detail(export_name);
			}
	        if (export_type == ExportEnum.entry_out.getId()) {
				workbook = this.exportExcelInfo_entry_detail(export_name);
			}
	        if (export_type == ExportEnum.balance.getId()) {
				workbook = this.exportExcelInfo_balance(export_name);
			}
	        if (export_type == ExportEnum.worker.getId()) {
				workbook = this.exportExcelInfo_worker(export_name);
			}
	        if (export_type == ExportEnum.supply.getId()) {
				workbook = this.exportExcelInfo_supply(export_name);
			}
	        if (export_type == ExportEnum.worker_fuhe.getId()) {
				workbook = this.exportExcelInfo_worker_fuhe(export_name);
			}
	        if (export_type == ExportEnum.supply_fuhe.getId()) {
				workbook = this.exportExcelInfo_supply_fuhe(export_name);
			}
	        if (export_type == ExportEnum.profit.getId()) {
				workbook = this.exportExcelInfo_profit(export_name);
			}
	        if (export_type == ExportEnum.profit_twelve.getId()) {
				workbook = this.exportExcelInfo_profit_twelve(export_name);
			}
	        if (export_type == ExportEnum.bill.getId()) {
				workbook = this.exportExcelInfo_bill(export_name);
			}
	        if (export_type == ExportEnum.management.getId()) {
				workbook = this.exportExcelInfo_management(export_name);
			}
	        OutputStream output;
	        try {
	            output = response.getOutputStream();
	            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
	            bufferedOutPut.flush();
	            workbook.write(bufferedOutPut);
	            bufferedOutPut.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        
	}
	    
	
	//接口功能:导入
	public void b_leading_get() throws Exception {
	    JSONObject obj_vars =  getObj_vars();
		int import_type = obj_vars.optInt("import_type");//
		String message = "";
	    if (import_type == ImportEnum.price_temp.getId()) {
	    	//价格模板数据导入
	    	 importExcelInfo_price_temp();
	    	 message = "导入成功";
		}
	    if (import_type == ImportEnum.price_temp_update.getId()) {
			//价格模板修改
	    	importExcelInfo_price_temp_update();
	    	message = "修改成功";
		}
	    if (import_type == ImportEnum.customer_supply.getId()) {
			//
	    	importExcelInfo_customer_supply();
	    	message = "导入成功";
		}
	    
	    results.setMessage(message);//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	    
	}
	
	
	//接口结束********************************************************************************************
	

	public void importExcelInfo_price_temp() throws Exception{
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String template_name = obj_vars.optString("template_name");///模板名称
		String http_url = obj_vars.optString("http_url");//长连接
		String url = obj_vars.optString("url");//url
		
		//插入主表
		Map<String, String> map = new HashMap<>();
		map.put("template_name", template_name);
		map.put("member_id", String.valueOf(member_id));
		map.put("url", url);
		map.put("http_url", http_url);
		
		int price_temp_id = sqlMapper.insert_or_update("p_price_temp", map, null);
		
	    List<List<Object>> listob = ExcelUtil.getExcel(this.rootPhyPath+url);
	    
	    //遍历listob数据，把数据放到List中
	    for (int i = 0; i < listob.size(); i++) {
	        List<Object> ob = listob.get(i);
	        if (!(ob.get(0).equals("") || ob.get(0) == null || ob.size() == 2)) {
	        	//设置编号
		        //通过遍历实现把每一列封装成一个model中，再把所有的model用List集合装载
		        String sql_item_id = "select item_id from p_item where item_name = '"+String.valueOf(ob.get(1))+"' limit 1";
		        String item_id = sqlMapper.public_one_scale(sql_item_id);
		        
		        String sql_sku_id = "select sku_id from p_sku where val_names = '"+String.valueOf(ob.get(2))+"' limit 1";
		        String sku_id = sqlMapper.public_one_scale(sql_sku_id);
		        
		        if (!StringUtil.isEmpty(item_id) && !StringUtil.isEmpty(sku_id)) {
		        	//插入
		        	String sql_count = "select count(1) from p_price_temp_detail "
		        			+ "where price_temp_id = "+price_temp_id+" and item_id = "+item_id+" and  sku_id = "+sku_id+"";
		        	String count = sqlMapper.public_one_scale(sql_count);
		        	if (Integer.parseInt(count) <= 0) {
		        		//新增数据
						String sql_insert = "insert into p_price_temp_detail (price_temp_id,item_id,sku_id,customer_price) values (%s,%s,%s,%s)";
						sqlMapper.insert(String.format(sql_insert, price_temp_id,item_id,sku_id,String.valueOf(ob.get(3))));
					} 
				}
			}
	        
	    }
	}
	
	public void importExcelInfo_price_temp_update() throws Exception{
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int price_temp_id = obj_vars.optInt("price_temp_id");//模板id
		String template_name = obj_vars.optString("template_name");///模板名称
		String http_url = obj_vars.optString("http_url");//长连接
		String url = obj_vars.optString("url");//url
		
		//删除从表
		String sql_delete_detail = "delete from p_price_temp_detail where price_temp_id = %s";
		sqlMapper.delete(String.format(sql_delete_detail, price_temp_id));
		
		//更新主表记录
		String sql_update = "update p_price_temp set member_id = %s, http_url = '%s' , url = '%s', template_name = '%s' , create_time = now() where price_temp_id = %s ";
		sqlMapper.update(String.format(sql_update,member_id, http_url, url, template_name, price_temp_id));
		
	    List<List<Object>> listob = ExcelUtil.getExcel(this.rootPhyPath+url);
	    
	    //遍历listob数据，把数据放到List中
	    for (int i = 0; i < listob.size(); i++) {
	        List<Object> ob = listob.get(i);
	        if (!(ob.get(0).equals("") || ob.get(0) == null || ob.size() == 2)) {
	        	//设置编号
		        //通过遍历实现把每一列封装成一个model中，再把所有的model用List集合装载
		        String sql_item_id = "select item_id from p_item where item_name = '"+String.valueOf(ob.get(1))+"' limit 1";
		        String item_id = sqlMapper.public_one_scale(sql_item_id);
		        
		        String sql_sku_id = "select sku_id from p_sku where val_names = '"+String.valueOf(ob.get(2))+"' limit 1";
		        String sku_id = sqlMapper.public_one_scale(sql_sku_id);
		        
		        if (!StringUtil.isEmpty(item_id) && !StringUtil.isEmpty(sku_id)) {
		        	//插入 (如果存在修改价格)
		        	String sql_count = "select count(1) from p_price_temp_detail "
		        			+ "where price_temp_id = "+price_temp_id+" and item_id = "+item_id+" and  sku_id = "+sku_id+"";
		        	String count = sqlMapper.public_one_scale(sql_count);
		        	if (Integer.parseInt(count) > 0) {
						//修改数据
		        		if (!StringUtil.isEmpty(String.valueOf(ob.get(3)))) {
		        			String sql_update_detail = "update p_price_temp_detail set customer_price = "+String.valueOf(ob.get(3))+" "
			        				+ "where price_temp_id = "+price_temp_id+" and item_id = "+item_id+" and  sku_id = "+sku_id+"";
						}
					} else {
						//新增数据
						String sql_insert = "insert into p_price_temp_detail (price_temp_id,item_id,sku_id,customer_price) values (%s,%s,%s,%s)";
						sqlMapper.insert(String.format(sql_insert, price_temp_id,item_id,sku_id,String.valueOf(ob.get(3))));
					}
				}
			}
	        
	    }
	}
	
	//{"action":"b_leading_get","mid":"1","customer_id":"189","import_type":"302"}
	public void importExcelInfo_customer_supply() throws Exception{
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		int customer_id = obj_vars.optInt("customer_id");//客户id
		String url = obj_vars.optString("url");//url
		
	    List<List<Object>> listob = ExcelUtil.getExcel(this.rootPhyPath+url);
	    //遍历listob数据，把数据放到List中
	    for (int i = 0; i < listob.size(); i++) {
	        List<Object> ob = listob.get(i);
	        
	        if (!(ob.get(0).equals("") || ob.get(0) == null || ob.size() == 2)) {
	        	//判断是否有数据
	        	 //设置编号
		        //通过遍历实现把每一列封装成一个model中，再把所有的model用List集合装载
		        String sql_catalog_id = "select catalog_id from p_catalog where catalog_name = '"+String.valueOf(ob.get(1))+"' limit 1";
		        String catalog_id = sqlMapper.public_one_scale(sql_catalog_id);
		        
		        String sql_sid = "select sid from p_supply where company = '"+String.valueOf(ob.get(2))+"' limit 1";
		        String sid = sqlMapper.public_one_scale(sql_sid);
		        
		        if (!StringUtil.isEmpty(catalog_id) && !StringUtil.isEmpty(sid)) {
		        	//插入 (如果存在修改价格)
		        	String sql_count = "select count(1) from p_customer_supply"
		        			+ " where cid = "+customer_id+" and catalog_id = "+catalog_id+" and sid = "+sid+" ";
		        	String count = sqlMapper.public_one_scale(sql_count);
		        	int is_default = 0;
        			if (String.valueOf(ob.get(3)).equals("是")) {
        				is_default = 1;
					}
		        	if (Integer.parseInt(count) > 0) {
						//修改数据
		        		if (!StringUtil.isEmpty(String.valueOf(ob.get(3)))) {
		        			String sql_update = "update p_customer_supply set is_default = "+is_default+"  "
			        				+ "where cid = "+customer_id+" and catalog_id = "+catalog_id+" and sid = "+sid+"";
						}
					} else {
						//新增数据
						String sql_insert = "insert into p_customer_supply (cid,catalog_id,sid,is_default) values (%s,%s,%s,%s)";
						sqlMapper.insert(String.format(sql_insert, customer_id,catalog_id,sid,is_default));
					}
				}
	        	
	        }
	        
	    }
	}
	
	
	public XSSFWorkbook exportExcelInfo_item(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException,
	IOException, SQLException{
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String item_name = obj_vars.optString("item_name");//商品名称
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//子类id
		String status = obj_vars.optString("status");
		
		String sql = "SELECT ps.sku_id, (select pcp.catalog_name from p_catalog pcp where pcp.catalog_id = pc.pid) catalog_name_parent, "
				+ " 	pc.catalog_name,pi.item_name,DATE_FORMAT(pi.deadline,'%H:%i') deadline,"
				+ " (case pi.status when 1 then '上架' when 2 then '下架' end) status "
				+ ",ps.val_names,pi.unit,ps.uniform_price "
				+ " FROM p_item pi "
				+ " left join p_catalog pc on pc.catalog_id = pi.catalog_id  "
				+ " left join p_sku ps on ps.item_id = pi.item_id "
				+ " WHERE 1=1 and pi.is_delete = 2 ";
				
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and pi.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id in (select catalog_id from p_catalog where pid = "+catalog_parent_id+")  ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id =  "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(status)) {
			sql+= " and pi.status= "+status+" ";
		}
		sql+=" ORDER BY pc.catalog_id, pi.item_id, ps.sku_id  ";
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
	    List<ItemBean> list = ItemBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("序号","sku_id",0));
	    excel.add(new ExcelBean("大类","catalog_name_parent",0));
	    excel.add(new ExcelBean("子类","catalog_name",0));
	    excel.add(new ExcelBean("商品名称","item_name",0));
	    excel.add(new ExcelBean("截止时间点","deadline",0));
	    excel.add(new ExcelBean("状态","status",0));
	    excel.add(new ExcelBean("规格","val_names",0));
	    excel.add(new ExcelBean("单位","unit",0));
	    excel.add(new ExcelBean("售价","uniform_price",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(ItemBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_sorting(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String item_name = obj_vars.optString("item_name");//商品名称
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(可空)
		String send_time = obj_vars.optString("send_time");//配送日期 2018-06-09
		
		String sql = "SELECT * FROM (select poi.order_item_id, poi.item_name, po.company kehu_company,  "
				+ " poi.val_names,poi.unit,poi.actual_num,poi.customer_note, "
				+ " DATE(po.send_time) send_time  from p_order_item poi "
				+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
				+ " inner join p_order po on po.oid = pod.oid "
				+ " where pod.order_type = 1 order by poi.item_id ) b "
				+ " WHERE 1=1 ";
		
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and b.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(send_time)) {
			sql+= " and b.send_time = '"+send_time+"' ";
		}
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
	    List<SortingBean> list = SortingBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("商品名称","item_name",0));
	    excel.add(new ExcelBean("客户名称","kehu_company",0));
	    excel.add(new ExcelBean("规格","val_names",0));
	    excel.add(new ExcelBean("单位","unit",0));
	    excel.add(new ExcelBean("数量","actual_num",0));
	    excel.add(new ExcelBean("客户备注","customer_note",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(SortingBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_supply_play(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String company = obj_vars.optString("company");//供应商名称(可空)
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(可空)
		String item_name = obj_vars.optString("item_name");//商品名称
		String send_time = obj_vars.optString("send_time");//配送日期 2018-06-09
		
		String sql = " SELECT * FROM (select poi.order_item_id, psu.company , po.company kehu_company, poi.item_name, "
				+ " poi.val_names,poi.unit,poi.actual_num,poi.customer_note, DATE(po.send_time) send_time  from p_order_item poi "
				+ " left join p_order_detail pod on pod.detail_oid = poi.detail_oid "
				+ " left join p_order po on po.oid = pod.oid  "
				+ " left join p_supply psu on psu.sid = poi.source_id "
				+ " where pod.order_type = 1 and poi.source_type = 2 order by psu.sid ) b WHERE 1=1 ";
		
		if (!StringUtil.isEmpty(company)) {
			sql+= " and  b.company like '%"+company+"%' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and b.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(send_time)) {
			sql+= " and b.send_time = '"+send_time+"' ";
		}
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
	    List<SupplyPlayBean> list = SupplyPlayBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("供应商名称","company",0));
	    excel.add(new ExcelBean("客户名称","kehu_company",0));
	    excel.add(new ExcelBean("商品名称","item_name",0));
	    excel.add(new ExcelBean("规格","val_names",0));
	    excel.add(new ExcelBean("单位","unit",0));
	    excel.add(new ExcelBean("数量","actual_num",0));
	    excel.add(new ExcelBean("客户备注","customer_note",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(SupplyPlayBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_diff(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(可空)
		String item_name = obj_vars.optString("item_name");//商品名称
		String send_time = obj_vars.optString("send_time");//配送日期 2018-06-09
		String diff_type = obj_vars.optString("diff_type");//销售属性 商品的差异类型：销售，上单，送货(汉字)
		
		String sql = "SELECT * FROM (select pod.order_diff_id, po.company kehu_company, pod.item_name, poi.val_names, pod.unit, pod.diff_num, "
				+ " (case pod.diff_type when 1 then '销售' when 2 then '上单' when 1 then '送货' end) diff_type, DATE(po.send_time) send_time  "
				+ "   from p_order_diff pod "
				+ " left join p_order po on po.oid = pod.oid "
				+ " order by po.create_time desc) b WHERE 1=1 ";
		
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and b.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and b.send_time like '%"+send_time+"%' ";
		}
		if (!StringUtil.isEmpty(diff_type)) {
			sql+= " and b.diff_type like '%"+diff_type+"%' ";
		}
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
	    List<DiffBean> list = DiffBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("客户名称","kehu_company",0));
	    excel.add(new ExcelBean("商品名称","item_name",0));
	    excel.add(new ExcelBean("规格","val_names",0));
	    excel.add(new ExcelBean("单位","unit",0));
	    excel.add(new ExcelBean("数量","diff_num",0));
	    excel.add(new ExcelBean("销售属性","diff_type",0));
	    excel.add(new ExcelBean("客户备注","customer_note",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(DiffBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_stock(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//子类id
		String item_name = obj_vars.optString("item_name");//商品名称
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String company = obj_vars.optString("company");//供应商名称(可空)
		String date_time = obj_vars.optString("date_time");//时间
		
		
		String sql = "select hip.inventory_id, (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pa.pid) catalog_parent_name, "
				+ " pa.catalog_name, pi.item_id, pi.item_name, hip.sku_id, hip.val_names, pi.unit, hip.qichu_num, hip.qichu_price, hip.qichu_amount, "
				+ " hip.should_num, hip.should_price, hip.should_amount, hip.real_num, hip.loss_num, hip.loss_price, hip.loss_amount, "
				+ " (hip.loss_amount/hip.should_amount) loss_rate, hip.today_purchase_amount, hip.today_order_amount "
				+ " from hx_inventory hip "
				+ " left join p_item pi on hip.item_id = pi.item_id "
				+ " left join p_catalog pa on pi.catalog_id = pa.catalog_id "
				+ " where 1=1  ";
		
		if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id in (select catalog_id from p_catalog where pid = "+catalog_parent_id+")  ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id =  "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and pi.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(date_time)) {
			sql+= " and DATE(hip.create_time) >= '"+date_time+"' and DATE(hip.create_time) <= '"+date_time+"' ";
		}
		sql+= " order by hip.inventory_id, hip.sku_id, pi.item_id ";
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
	    List<StockBean> list = StockBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("序号","inventory_id",0));
	    excel.add(new ExcelBean("大类","catalog_parent_name",0));
	    excel.add(new ExcelBean("子类","catalog_name",0));
	    excel.add(new ExcelBean("商品名称","item_name",0));
	    excel.add(new ExcelBean("规格","val_names",0));
	    excel.add(new ExcelBean("单位","unit",0));
	    excel.add(new ExcelBean("期初库存数量","qichu_num",0));
	    excel.add(new ExcelBean("期初库存单价","qichu_price",0));
	    excel.add(new ExcelBean("期初库存金额","qichu_amount",0));
	    excel.add(new ExcelBean("应库存数量","should_num",0));
	    excel.add(new ExcelBean("应库存单价","should_price",0));
	    excel.add(new ExcelBean("应库存金额","should_amount",0));
	    excel.add(new ExcelBean("实际库存数量","real_num",0));
	    excel.add(new ExcelBean("损耗数量","loss_num",0));
	    excel.add(new ExcelBean("损耗单价","loss_price",0));
	    excel.add(new ExcelBean("损耗金额","loss_amount",0));
	    excel.add(new ExcelBean("损耗率","loss_rate",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(StockBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_entry(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//子类id
		String item_name = obj_vars.optString("item_name");//商品名称
		
		String sql = " select hed.entry_detail_id, he.entry_id, he.entry_date, he.entry_no, "
				+ " (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pa.pid) catalog_parent_name, "
				+ " pa.catalog_name, pi.item_name, psku.val_names, pi.unit, hed.purchase_num, hed.purchase_price, hed.purchase_amount "
				+ " from hx_entry_detail hed "
				+ " left join hx_entry he on he.entry_id = hed.entry_id "
				+ " left join p_item pi on hed.item_id = pi.item_id "
				+ " left join p_catalog pa on pi.catalog_id = pa.catalog_id "
				+ " left join p_sku psku on hed.sku_id = psku.sku_id "
				+ " where 1=1 ";
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and he.entry_date >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and he.entry_date <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id in (select catalog_id from p_catalog where pid = "+catalog_parent_id+")  ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id =  "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and pi.item_name like '%"+item_name+"%' ";
		}
		sql+= " order by he.entry_id, hed.entry_detail_id  ";
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
	    List<EntryBean> list = EntryBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("日期","entry_date",0));
	    excel.add(new ExcelBean("大类","catalog_parent_name",0));
	    excel.add(new ExcelBean("子类","catalog_name",0));
	    excel.add(new ExcelBean("商品名称","item_name",0));
	    excel.add(new ExcelBean("规格","val_names",0));
	    excel.add(new ExcelBean("单位","unit",0));
	    excel.add(new ExcelBean("数量","purchase_num",0));
	    excel.add(new ExcelBean("平均价格","purchase_price",0));
	    excel.add(new ExcelBean("总价","purchase_amount",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(EntryBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_entry_detail(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String entry_no = obj_vars.optString("entry_no");//入库单号
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//子类id
		String item_name = obj_vars.optString("item_name");//商品名称
		String company = obj_vars.optString("company");//供应商名称(可空)
		String approve_status = obj_vars.optString("approve_status");//审核状态：1未审核；2审核通过；3审核拒绝
		
		String sql = " SELECT hed.entry_detail_id,he.entry_id, he.entry_no, entry_date, ps.company, "
				+ " (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pa.pid) catalog_parent_name, "
				+ " pa.catalog_name, pi.item_name, psku.val_names, pi.unit, hed.purchase_num, hed.purchase_price, hed.purchase_amount,"
				+ " (case he.approve_status when 1 then '未审核' when 2 then '审核通过' when 3 then '审核拒绝' end) approve_status "
				+ "  FROM hx_entry_detail hed "
				+ " left join hx_entry he on he.entry_id = hed.entry_id "
				+ " left join p_supply ps on he.sid = ps.sid "
				+ " left join p_item pi on hed.item_id = pi.item_id "
				+ " left join p_catalog pa on pi.catalog_id = pa.catalog_id "
				+ " left join p_sku psku on hed.sku_id = psku.sku_id "
				+ " WHERE 1=1  ";
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and he.entry_date >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and he.entry_date <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(entry_no)) {
			sql+= " and he.entry_no = "+entry_no+" ";
		}
		if (!StringUtil.isEmpty(approve_status)) {
			sql+= " and he.approve_status = "+approve_status+" ";
		}
		if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id in (select catalog_id from p_catalog where pid = "+catalog_parent_id+")  ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id =  "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and pi.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(company)) {
			sql+= " and ps.company like '%"+company+"%' ";
		}
		sql+= " order by he.entry_id, hed.entry_detail_id  ";
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
	    List<EntryDetailBean> list = EntryDetailBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("序号","entry_detail_id",0));
	    excel.add(new ExcelBean("单号","entry_no",0));
	    excel.add(new ExcelBean("日期","entry_date",0));
	    excel.add(new ExcelBean("供应商","company",0));
	    excel.add(new ExcelBean("大类","catalog_parent_name",0));
	    excel.add(new ExcelBean("子类","catalog_name",0));
	    excel.add(new ExcelBean("商品名称","item_name",0));
	    excel.add(new ExcelBean("规格","val_names",0));
	    excel.add(new ExcelBean("单位","unit",0));
	    excel.add(new ExcelBean("数量","purchase_num",0));
	    excel.add(new ExcelBean("采购价","purchase_price",0));
	    excel.add(new ExcelBean("总价","purchase_amount",0));
	    excel.add(new ExcelBean("总价","approve_status",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(EntryDetailBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_entry_out(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String order_no = obj_vars.optString("order_no");//订单号
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//子类id
		String item_name = obj_vars.optString("item_name");//商品名称
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String company = obj_vars.optString("company");//供应商名称(可空)
		
		String sql = "SELECT poi.order_item_id, po.company kehu_company, po.order_no,poi.item_name, "
				+ " DATE_FORMAT(po.send_time,'%Y-%m-%d %H:%i:%m') send_time,"
				+ " (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pa.pid) catalog_parent_name, "
				+ " pa.catalog_name, poi.val_names, poi.unit, poi.actual_num, poi.pay_price, (poi.actual_num*poi.pay_price) sum_price "
				+ " FROM p_order_item poi "
				+ " inner join p_order_detail pod on poi.detail_oid = pod.detail_oid "
				+ " left join p_order po on po.oid = pod.oid  "
				+ " left join p_item pi on poi.item_id = pi.item_id "
				+ " left join p_catalog pa on pi.catalog_id = pa.catalog_id "
				+ " WHERE 1=1 and pod.order_type = 1  and po.is_history = 0 ";
		
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(po.send_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(po.send_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(order_no)) {
			sql+= " and po.order_no <= '"+order_no+"' ";
		}
		if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id in (select catalog_id from p_catalog where pid = "+catalog_parent_id+")  ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and pi.catalog_id =  "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and pi.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and po.company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(company)) {
			sql+= " and poi.source_id in (select sid from p_supply where company like '%"+company+"%') ";
		}
		sql+= " order by po.oid, poi.order_item_id ";
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
	    List<EntryOut> list = EntryOut.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("序号","order_item_id",0));
	    excel.add(new ExcelBean("客户名称","kehu_company",0));
	    excel.add(new ExcelBean("单号","order_no",0));
	    excel.add(new ExcelBean("配送时间","send_time",0));
	    excel.add(new ExcelBean("大类","catalog_parent_name",0));
	    excel.add(new ExcelBean("子类","catalog_name",0));
	    excel.add(new ExcelBean("商品名称","item_name",0));
	    excel.add(new ExcelBean("规格","val_names",0));
	    excel.add(new ExcelBean("单位","unit",0));
	    excel.add(new ExcelBean("数量","actual_num",0));
	    excel.add(new ExcelBean("售价","pay_price",0));
	    excel.add(new ExcelBean("总价","sum_price",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(EntryOut.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_balance(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String status = obj_vars.optString("status"); //订单状态 (1:待收货2:已完成3:全部)
		String order_no = obj_vars.optString("order_no");//订单编号
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String salesman_name = obj_vars.optString("salesman_name");//业务员名称
		String deliver_name = obj_vars.optString("deliver_name");//配送员名称
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		String sql = "SELECT po.oid, DATE_FORMAT(po.create_time,'%Y-%m-%d') create_time, po.company kehu_company,"
				+ "  hm.member_name salesman_name, hmd.member_name deliver_name"
				+ " , po.order_no, po.status, po.real_pay_amount, po.virtual_pay_amount, po.diff_amount, "
				+ " (case po.jiesuan_status when 1 then '未结算' when 2 then '已结算' end) jiesuan_status, "
				+ "  if((po.status='已完成' and po.jiesuan_status = 1) ,1,2) is_jiesuan   "
				+ "  FROM p_order po  "
				+ " left join p_customer pc on po.cid = pc.cid"
				+ " left join hx_member  hm on pc.salesman_id = hm.member_id "
				+ " left join hx_member hmd on pc.deliver_id = hmd.member_id "
				+ " WHERE 1=1 and po.is_history = 0 ";
		if (!StringUtil.isEmpty(status)) {
			switch (status) {
			case "1":
				sql+= " and po.status = '待收货' ";
				break;
			case "2":
				sql+= " and po.status = '已完成' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(order_no)) {
			sql+= " and po.order_no = '"+order_no+"' ";
		}
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(po.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(po.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and po.company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(salesman_name)) {
			sql+= " and hm.member_name like '%"+salesman_name+"%' ";
		}
		if (!StringUtil.isEmpty(deliver_name)) {
			sql+= " and hmd.member_name like '%"+deliver_name+"%' ";
		}
		sql+= " order by po.create_time desc  ";
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
	    List<BalanceBean> list = BalanceBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("日期","create_time",0));
	    excel.add(new ExcelBean("客户名称","kehu_company",0));
	    excel.add(new ExcelBean("业务员","salesman_name",0));
	    excel.add(new ExcelBean("配送员","deliver_name",0));
	    excel.add(new ExcelBean("订单编号","order_no",0));
	    excel.add(new ExcelBean("订单状态","status",0));
	    excel.add(new ExcelBean("实际订单金额","real_pay_amount",0));
	    excel.add(new ExcelBean("虚拟订单金额","virtual_pay_amount",0));
	    excel.add(new ExcelBean("差额","diff_amount",0));
	    excel.add(new ExcelBean("单据状态","jiesuan_status",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(BalanceBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_worker(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String hexiao_status = obj_vars.optString("hexiao_status");//核销状态：1未核销；2已核销;3全部
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String order_no = obj_vars.optString("order_no");//订单编号
		String member_name = obj_vars.optString("member_name");//师傅名称
		String item_name = obj_vars.optString("item_name");//商品名称
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		
		String sql = " SELECT * FROM ( select poi.order_item_id, po.oid, DATE_FORMAT(po.create_time,'%Y-%m-%d %H:%i:%m')"
				+ " create_time, po.order_no, hm.member_name, po.company kehu_company, "
				+ " (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pca.pid) catalog_parent_name,"
				+ " pca.catalog_name, poi.item_name, poi.val_names, "
				+ " poi.unit, poi.order_num, poi.actual_num, poi.pay_price customer_price, (poi.actual_num*poi.pay_price) money , "
				+ " poi.actual_num actual_num_member, poi.cost_price, (poi.actual_num*poi.cost_price) cost_money, "
				+ " (poi.actual_num*poi.pay_price-poi.actual_num*poi.cost_price) profit , "
				+ " (case poi.oi_type when 1 then '销售' when 2 then '送货' when 3 then '上单' end) diff_type, "
				+ " IF((select COUNT(1) from hx_fin_worker_detail hfwd inner join hx_fin_worker hfw "
				+ "on hfw.fin_worker_id = hfwd.fin_worker_id  where hfwd.oid = po.oid and hfw.is_history = 0) > 0,'已核销','未核销') hexiao_status, "
				+ " IF((select COUNT(1) from hx_fin_worker_detail hfwd inner join hx_fin_worker hfw "
				+ "on hfw.fin_worker_id = hfwd.fin_worker_id  where hfwd.oid = po.oid and hfw.is_history = 0) > 0, "
				+ " (select hfwd.hexiao_note from hx_fin_worker_detail hfwd where hfwd.order_item_id = poi.order_item_id),'') hexiao_note "
				+ " from p_order_item poi "
				+ " inner join p_order_detail pod on poi.detail_oid = pod.detail_oid "
				+ " inner join p_order po on po.oid = pod.oid "
				+ " left join p_item pi on poi.item_id = pi.item_id "
				+ " left join p_catalog pca on pi.catalog_id = pca.catalog_id "
				+ " left join hx_member hm on hm.member_id = poi.source_id "
				+ " where pod.order_type = 1 and poi.source_type = 3 and po.is_history = 0 and po.status = '已完成'  ) a "
				+ " WHERE 1=1  ";//poi.source_type = 3
		
		if (!StringUtil.isEmpty(hexiao_status)) {
			switch (hexiao_status) {
			case "1":
				sql+= " and a.hexiao_status = '未核销' ";
				break;
			case "2":
				sql+= " and a.hexiao_status = '已核销' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(a.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(a.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(order_no)) {
			sql+= " and a.order_no = '"+order_no+"' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and a.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and a.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(member_name)) {
			sql+= " and a.member_name like '%"+member_name+"%' ";
		}
		sql += " order by a.order_item_id ";		
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
	    List<WorkerBean> list = WorkerBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("订单时间","create_time",0));
	    excel.add(new ExcelBean("订单编号","order_no",0));
	    excel.add(new ExcelBean("师傅名称","member_name",0));
	    excel.add(new ExcelBean("客户名称","kehu_company",0));
	    excel.add(new ExcelBean("大类","catalog_parent_name",0));
	    excel.add(new ExcelBean("子类","catalog_name",0));
	    excel.add(new ExcelBean("商品名称","item_name",0));
	    excel.add(new ExcelBean("规格","val_names",0));
	    excel.add(new ExcelBean("单位","unit",0));
	    excel.add(new ExcelBean("下单数量","order_num",0));
	    excel.add(new ExcelBean("实收数量","actual_num",0));
	    excel.add(new ExcelBean("售价","customer_price",0));
	    excel.add(new ExcelBean("金额","money",0));
	    excel.add(new ExcelBean("师傅临采数量","actual_num_member",0));
	    excel.add(new ExcelBean("师傅临采价格","cost_price",0));
	    excel.add(new ExcelBean("师傅临采金额","cost_money",0));
	    excel.add(new ExcelBean("利润","profit",0));
	    excel.add(new ExcelBean("单据备注","hexiao_status",0));
	    excel.add(new ExcelBean("单据状态","hexiao_note",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(WorkerBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_supply(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String hexiao_status = obj_vars.optString("hexiao_status");//核销状态：1未核销；2已核销;3全部
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String company = obj_vars.optString("company");//供应商名
		
		String sql = "SELECT a.supply_id, a.company,a.create_time, a.cost_price,"
				+ " IF ((select count(1) from hx_fin_supply where sid = a.supply_id  and  fin_supply_date = a.create_time) > 0,'已核销','未核销') hexiao_status "
				+ " FROM "
				+ " ( "
				+ " select poi.source_id supply_id,(select company from p_supply where sid = poi.source_id) company, "
				+ " DATE(poi.create_time) create_time, sum(poi.actual_num*poi.cost_price) cost_price "
				+ " from p_order_item poi "
				+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
				+ " inner join p_order po on pod.oid = po.oid  "
				+ " where poi.source_type = 2 and pod.order_type = 1 and po.status = '已完成' group by poi.source_id, DATE(poi.create_time)"
				+ " ) a  ";
		
		if (!StringUtil.isEmpty(hexiao_status)) {
			switch (hexiao_status) {
			case "1":
				sql+= " and a.hexiao_status = '未核销' ";
				break;
			case "2":
				sql+= " and a.hexiao_status = '已核销' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(a.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(a.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(company)) {
			sql+= " and a.company like '%"+company+"%' ";
		}
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
	    List<SupplyBean> list = SupplyBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("日期","create_time",0));
	    excel.add(new ExcelBean("供应商名称","company",0));
	    excel.add(new ExcelBean("采购总金额","cost_price",0));
	    excel.add(new ExcelBean("单据状态","hexiao_status",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(SupplyBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_worker_fuhe(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String fuhe_status = obj_vars.optString("fuhe_status");//核销状态：1未复核；2已复核;3全部
		String start_time_order = obj_vars.optString("start_time_order");//开始时间 2018-10-10
		String end_time_order = obj_vars.optString("end_time_order");//结束时间  2018-10-10
		String start_time_fuhe = obj_vars.optString("start_time_fuhe");//开始时间 2018-10-10
		String end_time_fuhe = obj_vars.optString("end_time_fuhe");//结束时间  2018-10-10
		String fin_worker_no = obj_vars.optString("fin_worker_no");//结算单流水号
		String order_no = obj_vars.optString("order_no");//订单编号
		String member_name = obj_vars.optString("member_name");//师傅名称
		String kehu_company = obj_vars.optString("kehu_company");//客户名称(修改)
		String item_name = obj_vars.optString("item_name");//商品名称
		
		//先分页
		String sql_limit = " select GROUP_CONCAT(fin_worker_id) g from hx_fin_worker order by  fin_worker_id desc ";
		String in_nums = sqlMapper.public_one_scale(sql_limit);
		//在查数据
		
		String sql = "select * from (select hfw.fin_worker_id, hfwd.fin_worker_detail_id, hfw.fin_worker_no, "
				+ "  DATE_FORMAT(po.create_time,'%Y-%m-%d %H:%i:%m') order_time, po.order_no, hm.member_name, po.company kehu_company,"
				+ " (select pcl.catalog_name from p_catalog pcl where pcl.catalog_id = pca.pid) catalog_parent_name, pca.catalog_name, hfwd.order_item_id, "
				+ " poi.item_name, poi.val_names,poi.unit, poi.order_num, poi.actual_num, poi.customer_price, (poi.actual_num*poi.customer_price) money , "
				+ " poi.cost_price, (poi.actual_num*poi.cost_price) cost_money, (poi.actual_num*poi.customer_price-poi.actual_num*poi.cost_price) profit ,"
				+ " hfwd.fuhe_note,(case hfw.fuhe_status when 1 then '未复核' when 2 then '已复核' end) fuhe_status, "
				+ " DATE_FORMAT(hfw.fuhe_time,'%Y-%m-%d %H:%i:%m') fuhe_time, IF(hfw.fuhe_member_id is null,'', "
				+ " (select member_name from hx_member where member_id = hfw.fuhe_member_id)) fuhe_name "
				+ "  from hx_fin_worker_detail hfwd "
				+ " left join p_order po on po.oid = hfwd.oid"
				+ " left join p_order_item poi on poi.order_item_id = hfwd.order_item_id"
				+ " left join hx_member hm on poi.source_id = hm.member_id"
				+ " left join p_item pi on poi.item_id = pi.item_id"
				+ " left join p_catalog pca on pi.catalog_id = pca.catalog_id"
				+ " left join hx_fin_worker hfw on hfw.fin_worker_id = hfwd.fin_worker_id "
				+ " where hfw.is_history = 0 and po.is_history = 0  order by hfw.fin_worker_id desc ) b "
				+ " where 1=1 and b.fin_worker_id in ("+in_nums+") "; //is_history 不是历史订单

		if (!StringUtil.isEmpty(fuhe_status)) {
			switch (fuhe_status) {
			case "1":
				sql+= " and b.fuhe_status = '未复核' ";
				break;
			case "2":
				sql+= " and b.fuhe_status = '已复核' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(start_time_order)) {
			sql+= " and DATE(b.order_time) >= '"+start_time_order+"' ";
		}
		if (!StringUtil.isEmpty(end_time_order)) {
			sql+= " and DATE(b.order_time) <= '"+end_time_order+"' ";
		}
		if (!StringUtil.isEmpty(start_time_fuhe)) {
			sql+= " and DATE(b.fuhe_time) >= '"+start_time_fuhe+"' ";
		}
		if (!StringUtil.isEmpty(end_time_fuhe)) {
			sql+= " and DATE(b.fuhe_time) <= '"+end_time_fuhe+"' ";
		}
		if (!StringUtil.isEmpty(fin_worker_no)) {
			sql+= " and b.fin_worker_no = '"+fin_worker_no+"' ";
		}
		if (!StringUtil.isEmpty(order_no)) {
			sql+= " and b.order_no = '"+order_no+"' ";
		}
		if (!StringUtil.isEmpty(member_name)) {
			sql+= " and b.member_name like '%"+member_name+"%' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and b.item_name like '%"+item_name+"%' ";
		}
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
	    List<WorkerFuheBean> list = WorkerFuheBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("结算单流水号","fin_worker_no",0));
	    excel.add(new ExcelBean("订单时间","order_time",0));
	    excel.add(new ExcelBean("订单编号","order_no",0));
	    excel.add(new ExcelBean("师傅名称","member_name",0));
	    excel.add(new ExcelBean("客户名称","kehu_company",0));
	    excel.add(new ExcelBean("大类","catalog_parent_name",0));
	    excel.add(new ExcelBean("子类","catalog_name",0));
	    excel.add(new ExcelBean("商品名称","item_name",0));
	    excel.add(new ExcelBean("规格","val_names",0));
	    excel.add(new ExcelBean("单位","unit",0));
	    excel.add(new ExcelBean("下单数量","order_num",0));
	    excel.add(new ExcelBean("实收数量","actual_num",0));
	    excel.add(new ExcelBean("售价","customer_price",0));
	    excel.add(new ExcelBean("金额","money",0));
	    excel.add(new ExcelBean("师傅临采价","cost_price",0));
	    excel.add(new ExcelBean("师傅临采金额","cost_money",0));
	    excel.add(new ExcelBean("利润","profit",0));
	    excel.add(new ExcelBean("单据备注","fuhe_note",0));
	    excel.add(new ExcelBean("单据状态","fuhe_status",0));
	    excel.add(new ExcelBean("复核日期","fuhe_time",0));
	    excel.add(new ExcelBean("复核人","fuhe_name",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(WorkerFuheBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_supply_fuhe(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String fuhe_status = obj_vars.optString("fuhe_status");//复核状态：1未复核；2已复核;3全部
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String fin_supply_no = obj_vars.optString("fin_supply_no");//结算单流水号
		String company = obj_vars.optString("company");//供应商名
		
		String sql = " SELECT * FROM ( select hfs.fin_supply_id,hfs.fin_supply_no, hfs.fin_supply_date, ps.company, hfs.fin_supply_amount, "
				+ " (case hfs.fuhe_status when 1 then '未复核' when 2 then '已复核' end) fuhe_status, "
				+ " IF(hfs.fuhe_member_id is null, '', (select member_name from hx_member where member_id = hfs.fuhe_member_id)) fuhe_name, "
				+ " DATE_FORMAT(hfs.fuhe_time,'%Y-%m-%d %H:%i:%m') fuhe_time  from hx_fin_supply hfs "
				+ " left join p_supply ps on ps.sid = hfs.sid "
				+ " where hfs.is_history = 0 order by hfs.fin_supply_id desc) b"
				+ " WHERE 1=1 ";
		
		if (!StringUtil.isEmpty(fuhe_status)) {
			switch (fuhe_status) {
			case "1":
				sql+= " and b.fuhe_status = '未复核' ";
				break;
			case "2":
				sql+= " and b.fuhe_status = '已复核' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(b.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(b.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(fin_supply_no)) {
			sql+= " and b.fin_supply_no >= '"+fin_supply_no+"' ";
		}
		if (!StringUtil.isEmpty(company)) {
			sql+= " and b.company like '%"+company+"%' ";
		}
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
	    List<SupplyFuheBean> list = SupplyFuheBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("结算单流水号","fin_supply_no",0));
	    excel.add(new ExcelBean("日期","fin_supply_date",0));
	    excel.add(new ExcelBean("供应商名称","company",0));
	    excel.add(new ExcelBean("采购总金额","fin_supply_amount",0));
	    excel.add(new ExcelBean("单据状态","fuhe_status",0));
	    excel.add(new ExcelBean("复核人","fuhe_name",0));
	    excel.add(new ExcelBean("复核时间","fuhe_time",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(SupplyFuheBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_profit(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
	    //根据条件查询数据，把数据装载到一个list中
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String salesman_name = obj_vars.optString("salesman_name");//业务员名称
		
		
		String sql = "SELECT b.cid, b.kehu_company, b.salesman_name, b.sales_volume,  b.sales_cost, (b.sales_volume - b.sales_cost) sum_profit, "
				+ "  ROUND(((b.sales_volume - b.sales_cost)/b.sales_volume*100),2) profit_rate  "
				+ "  FROM (select po.cid, po.company kehu_company, SUM(po.real_pay_amount) sales_volume, hm.member_name salesman_name, "
				+ " (select sum(poi.cost_price*poi.actual_num) from p_order_item poi inner join p_order_detail pod on "
				+ "  pod.detail_oid = poi.detail_oid where pod.oid = po.oid) sales_cost, po.create_time "
				+ " from p_order po"
				+ " left join p_customer pc on pc.cid = po.cid "
				+ " left join hx_member hm on hm.member_id = pc.salesman_id "
				+ " where po.is_history = 0 and po.status = '已完成' "
				+ " group by po.cid order by cid) b "
				+ " WHERE 1=1    ";
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(b.create_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(b.create_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(salesman_name)) {
			sql+= " and b.salesman_name like '%"+salesman_name+"%' ";
		}
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
		for (Map<String, Object> map : list_sql) {
			map.put("time", start_time+"——"+end_time);
		}
		
		
	    List<ProfitBean> list = ProfitBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("序号","cid",0));
	    excel.add(new ExcelBean("客户名称","kehu_company",0));
	    excel.add(new ExcelBean("业务员","salesman_name",0));
	    excel.add(new ExcelBean("销售额","sales_volume",0));
	    excel.add(new ExcelBean("成本","sales_cost",0));
	    excel.add(new ExcelBean("利润","sum_profit",0));
	    excel.add(new ExcelBean("利润率","profit_rate",0));
	    excel.add(new ExcelBean("账期","time",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(ProfitBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	public XSSFWorkbook exportExcelInfo_profit_twelve(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		int is_print = obj_vars.optInt("is_print");//是否打印(1:是2:不是) 查数据就传不是
		
		//查询客户
		String sql_customer = "select cid, company from p_customer ";
		if (!StringUtil.isEmpty(kehu_company)) {
			sql_customer+= " where company like '%"+kehu_company+"%' ";
		}
		sql_customer+= " order by cid ";
		List<Map<String, Object>> customer_list = sqlMapper.public_list_all_sql(sql_customer);
		
		
		String sql = "select b.cid, b.kehu_company, b.salesman_name, ifnull(sum(b.sales_volume),0) sales_volume,  "
				+ " ifnull(sum(b.sales_cost),0) sales_cost, ifnull(sum(b.sales_volume - b.sales_cost),0) sum_profit, "
				+ "  ifnull(ROUND(((b.sales_volume - b.sales_cost)/b.sales_volume*100),2),0) profit_rate  "
				+ ",year(b.create_time) year,month(b.create_time) month "
				+ " from(select po.cid, po.company kehu_company, SUM(po.real_pay_amount) sales_volume, hm.member_name salesman_name, "
				+ " (select sum(poi.cost_price*poi.actual_num) from p_order_item poi inner join p_order_detail pod on "
				+ "  pod.detail_oid = poi.detail_oid where pod.oid = po.oid) sales_cost, po.create_time "
				+ " from p_order po "
				+ " left join p_customer pc on pc.cid = po.cid   "
				+ " left join hx_member hm on hm.member_id = pc.salesman_id "
				+ " group by po.cid order by cid) b "
				+ "where year(b.create_time) = (SELECT DATE_FORMAT(NOW(), '%Y')) ";
				
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and b.kehu_company like '%"+kehu_company+"%' ";
		}
		sql+=" group by b.cid,year(b.create_time), month(b.create_time) order by month(b.create_time) asc, b.cid asc ";
		
		
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(sql);
		
		//数据转换
		for (Map<String, Object> map_customer : customer_list) {
			//主cid
			int customer_cid = Integer.valueOf(map_customer.get("cid").toString());
			
			for (Map<String, Object> map : list) {
				int month_cid = Integer.valueOf(map.get("cid").toString());
				int month = Integer.valueOf(map.get("month").toString());
				if (customer_cid == month_cid) {
					if (month == 1) {
						map_customer.put("sales_volume_1", map.get("sales_volume"));
						map_customer.put("sales_cost_1", map.get("sales_cost"));
						map_customer.put("sum_profit_1", map.get("sum_profit"));
						map_customer.put("profit_rate_1", map.get("profit_rate"));
					}
					if (month == 2) {
						map_customer.put("sales_volume_2", map.get("sales_volume"));
						map_customer.put("sales_cost_2", map.get("sales_cost"));
						map_customer.put("sum_profit_2", map.get("sum_profit"));
						map_customer.put("profit_rate_2", map.get("profit_rate"));
					}
					if (month == 3) {
						map_customer.put("sales_volume_3", map.get("sales_volume"));
						map_customer.put("sales_cost_3", map.get("sales_cost"));
						map_customer.put("sum_profit_3", map.get("sum_profit"));
						map_customer.put("profit_rate_3", map.get("profit_rate"));
					}
					if (month == 4) {
						map_customer.put("sales_volume_4", map.get("sales_volume"));
						map_customer.put("sales_cost_4", map.get("sales_cost"));
						map_customer.put("sum_profit_4", map.get("sum_profit"));
						map_customer.put("profit_rate_4", map.get("profit_rate"));
					}
					if (month == 5) {
						map_customer.put("sales_volume_6", map.get("sales_volume"));
						map_customer.put("sales_cost_6", map.get("sales_cost"));
						map_customer.put("sum_profit_6", map.get("sum_profit"));
						map_customer.put("profit_rate_6", map.get("profit_rate"));
					}
					if (month == 7) {
						map_customer.put("sales_volume_7", map.get("sales_volume"));
						map_customer.put("sales_cost_7", map.get("sales_cost"));
						map_customer.put("sum_profit_7", map.get("sum_profit"));
						map_customer.put("profit_rate_7", map.get("profit_rate"));
					}
					if (month == 8) {
						map_customer.put("sales_volume_8", map.get("sales_volume"));
						map_customer.put("sales_cost_8", map.get("sales_cost"));
						map_customer.put("sum_profit_8", map.get("sum_profit"));
						map_customer.put("profit_rate_8", map.get("profit_rate"));
					}
					if (month == 9) {
						map_customer.put("sales_volume_9", map.get("sales_volume"));
						map_customer.put("sales_cost_9", map.get("sales_cost"));
						map_customer.put("sum_profit_9", map.get("sum_profit"));
						map_customer.put("profit_rate_9", map.get("profit_rate"));
					}
					if (month == 10) {
						map_customer.put("sales_volume_10", map.get("sales_volume"));
						map_customer.put("sales_cost_10", map.get("sales_cost"));
						map_customer.put("sum_profit_10", map.get("sum_profit"));
						map_customer.put("profit_rate_10", map.get("profit_rate"));
					}
					if (month == 11) {
						map_customer.put("sales_volume_11", map.get("sales_volume"));
						map_customer.put("sales_cost_11", map.get("sales_cost"));
						map_customer.put("sum_profit_11", map.get("sum_profit"));
						map_customer.put("profit_rate_11", map.get("profit_rate"));
					}
					if (month == 12) {
						map_customer.put("sales_volume_12", map.get("sales_volume"));
						map_customer.put("sales_cost_12", map.get("sales_cost"));
						map_customer.put("sum_profit_12", map.get("sum_profit"));
						map_customer.put("profit_rate_12", map.get("profit_rate"));
					}
				}
			}
			
		}
		
	    List<ProfitTwelveBean> list_twelve = ProfitTwelveBean.getBean(customer_list);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("序号","cid",0));
	    excel.add(new ExcelBean("客户名称","company",0));
	    
	    excel.add(new ExcelBean("1月销售额","sales_volume_1",0));
	    excel.add(new ExcelBean("1月成本","sales_cost_1",0));
	    excel.add(new ExcelBean("1月利润","sum_profit_1",0));
	    excel.add(new ExcelBean("1月利润率","profit_rate_1",0));
	    
	    excel.add(new ExcelBean("2月销售额","sales_volume_2",0));
	    excel.add(new ExcelBean("2月成本","sales_cost_2",0));
	    excel.add(new ExcelBean("2月利润","sum_profit_2",0));
	    excel.add(new ExcelBean("2月利润率","profit_rate_2",0));
	    
	    excel.add(new ExcelBean("3月销售额","sales_volume_3",0));
	    excel.add(new ExcelBean("3月成本","sales_cost_3",0));
	    excel.add(new ExcelBean("3月利润","sum_profit_3",0));
	    excel.add(new ExcelBean("3月利润率","profit_rate_3",0));
	    
	    excel.add(new ExcelBean("4月销售额","sales_volume_4",0));
	    excel.add(new ExcelBean("4月成本","sales_cost_4",0));
	    excel.add(new ExcelBean("4月利润","sum_profit_4",0));
	    excel.add(new ExcelBean("4月利润率","profit_rate_4",0));
	    
	    excel.add(new ExcelBean("5月销售额","sales_volume_5",0));
	    excel.add(new ExcelBean("5月成本","sales_cost_5",0));
	    excel.add(new ExcelBean("5月利润","sum_profit_5",0));
	    excel.add(new ExcelBean("5月利润率","profit_rate_5",0));
	    
	    excel.add(new ExcelBean("6月销售额","sales_volume_6",0));
	    excel.add(new ExcelBean("6月成本","sales_cost_6",0));
	    excel.add(new ExcelBean("6月利润","sum_profit_6",0));
	    excel.add(new ExcelBean("6月利润率","profit_rate_6",0));
	    
	    excel.add(new ExcelBean("7月销售额","sales_volume_7",0));
	    excel.add(new ExcelBean("7月成本","sales_cost_7",0));
	    excel.add(new ExcelBean("7月利润","sum_profit_7",0));
	    excel.add(new ExcelBean("7月利润率","profit_rate_7",0));
	    
	    excel.add(new ExcelBean("8月销售额","sales_volume_8",0));
	    excel.add(new ExcelBean("8月成本","sales_cost_8",0));
	    excel.add(new ExcelBean("8月利润","sum_profit_8",0));
	    excel.add(new ExcelBean("8月利润率","profit_rate_8",0));
	    
	    excel.add(new ExcelBean("9月销售额","sales_volume_9",0));
	    excel.add(new ExcelBean("9月成本","sales_cost_9",0));
	    excel.add(new ExcelBean("9月利润","sum_profit_9",0));
	    excel.add(new ExcelBean("9月利润率","profit_rate_9",0));
	    
	    excel.add(new ExcelBean("10月销售额","sales_volume_10",0));
	    excel.add(new ExcelBean("10月成本","sales_cost_10",0));
	    excel.add(new ExcelBean("10月利润","sum_profit_10",0));
	    excel.add(new ExcelBean("10月利润率","profit_rate_10",0));
	    
	    excel.add(new ExcelBean("11月销售额","sales_volume_11",0));
	    excel.add(new ExcelBean("11月成本","sales_cost_11",0));
	    excel.add(new ExcelBean("11月利润","sum_profit_11",0));
	    excel.add(new ExcelBean("11月利润率","profit_rate_11",0));
	    
	    excel.add(new ExcelBean("12月销售额","sales_volume_12",0));
	    excel.add(new ExcelBean("12月成本","sales_cost_12",0));
	    excel.add(new ExcelBean("12月利润","sum_profit_12",0));
	    excel.add(new ExcelBean("12月利润率","profit_rate_12",0));
	    
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(ProfitTwelveBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	//账单格式
	public XSSFWorkbook exportExcelInfo_bill(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		String sql = "select item_name, unit from p_item where is_delete = 2 and status = 1 order by ordid";
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
		
	    List<BillBean> list = BillBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("商品名称","item_name",0));
	    excel.add(new ExcelBean("单位","unit",0));
	    excel.add(new ExcelBean("送货数量","nums",0));
	    excel.add(new ExcelBean("单价","price",0));
	    excel.add(new ExcelBean("金额","amount",0));
	    excel.add(new ExcelBean("备注","remark",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(BillBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	//账单格式
	public XSSFWorkbook exportExcelInfo_management(String export_name) throws 
	InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		
		int member_id = obj_vars.optInt("mid");//后台登录用户id
		String hexiao_status = obj_vars.optString("hexiao_status");//核销状态：1未核销；2已核销;3全部
		String fuhe_status = obj_vars.optString("fuhe_status");//复核状态：1未复核；2已复核;3全部
		String start_time = obj_vars.optString("start_time");//开始时间 2018-10-10
		String end_time = obj_vars.optString("end_time");//结束时间  2018-10-10
		String kehu_company = obj_vars.optString("kehu_company");//客户名称
		String company = obj_vars.optString("company");//供应商名
		String catalog_parent_id = obj_vars.optString("catalog_parent_id");//大类id
		String catalog_id = obj_vars.optString("catalog_id");//子类id
		String item_name = obj_vars.optString("item_name");//商品名称
		String deliver_name = obj_vars.optString("deliver_name");//配送员名称
		String salesman_name = obj_vars.optString("salesman_name");//业务员名称
		
		String sql = "SELECT * FROM (select hfsd.fin_supply_detail_id, hfs.fin_supply_no, "
				+ "if(po.jiesuan_time is null,'', DATE_FORMAT(po.jiesuan_time,'%Y-%m-%d %H:%i:%m')) jiesuan_time, hfs.fin_supply_date, "
				+ " DATE_FORMAT(po.create_time,'%Y-%m-%d %H:%i:%m') order_time, po.order_no, "
				+ " pc.company kehu_company, hm.member_name deliver_name, hms.member_name salesman_name ,  pca.pid catalog_parent_id, "
				+ " (select catalog_name from p_catalog pcl where pcl.catalog_id = pca.pid) catalog_parent_name, "
				+ "pca.catalog_id, pca.catalog_name, poi.item_name, poi.val_names, poi.unit, poi.actual_num,  "
				+ " poi.pay_price customer_price, (poi.actual_num* poi.pay_price) money, "
				+ " (case poi.oi_type when 1 then '销售' when 2 then '送货' when 3 then '上单' end) diff_type, "
				+ " poi.customer_note, poi.cost_price, (poi.cost_price*poi.actual_num) sum_cost, "
				+ "(poi.pay_price-poi.cost_price*poi.actual_num) profit, hfsd.hexiao_note , "
				+ " ps.sid, ps.company, (case hfs.fuhe_status when 1 then '未复核' when 2 then '已复核' end)"
				+ " fuhe_status, DATE_FORMAT(hfs.fuhe_time,'%Y-%m-%d %H:%i:%m') fuhe_time, "
				+ " if(hfs.fuhe_member_id is null,'',(select member_name from hx_member where member_id = hfs.fuhe_member_id)) fuhe_member_name "
				+ "  from hx_fin_supply_detail hfsd "
				+ " left join hx_fin_supply hfs on hfs.fin_supply_id = hfsd.fin_supply_id "
				+ " left join p_order po on po.oid = hfsd.oid "
				+ " left join p_customer pc on pc.cid = po.cid"
				+ " left join hx_member hm on hm.member_id = pc.deliver_id"
				+ " left join hx_member hms on hms.member_id = pc.salesman_id "
				+ " left join p_order_item poi on poi.order_item_id = hfsd.order_item_id "
				+ " left join p_item pi on pi.item_id = pi.item_id "
				+ " left join p_catalog pca on pca.catalog_id = pi.catalog_id "
				+ " left join p_supply ps on ps.sid = poi.source_id"
				+ " where poi.source_type = 2 and hfs.is_history = 0 and po.is_history = 0 GROUP BY fin_supply_detail_id ) a "
				+ " WHERE 1=1  ";
		
		if (!StringUtil.isEmpty(hexiao_status)) {
			switch (hexiao_status) {
			case "1":
				sql+= " and a.hexiao_status = '未核销' ";
				break;
			case "2":
				sql+= " and a.hexiao_status = '已核销' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(fuhe_status)) {
			switch (fuhe_status) {
			case "1":
				sql+= " and a.fuhe_status = '未复核' ";
				break;
			case "2":
				sql+= " and a.fuhe_status = '已复核' ";
				break;
			case "3":
				break;
			}
		}
		if (!StringUtil.isEmpty(start_time)) {
			sql+= " and DATE(a.hexiao_time) >= '"+start_time+"' ";
		}
		if (!StringUtil.isEmpty(end_time)) {
			sql+= " and DATE(a.hexiao_time) <= '"+end_time+"' ";
		}
		if (!StringUtil.isEmpty(kehu_company)) {
			sql+= " and a.kehu_company like '%"+kehu_company+"%' ";
		}
		if (!StringUtil.isEmpty(company)) {
			sql+= " and a.company like '%"+company+"%' ";
		}
		if (!StringUtil.isEmpty(catalog_parent_id) && StringUtil.isEmpty(catalog_id)) {
			sql+= " and a.catalog_parent_id = "+catalog_parent_id+" ";
		}
		if (!StringUtil.isEmpty(catalog_id)) {
			sql+= " and a.catalog_id =  "+catalog_id+" ";
		}
		if (!StringUtil.isEmpty(item_name)) {
			sql+= " and a.item_name like '%"+item_name+"%' ";
		}
		if (!StringUtil.isEmpty(salesman_name)) {
			sql+= " and a.salesman_name like '%"+salesman_name+"%' ";
		}
		if (!StringUtil.isEmpty(deliver_name)) {
			sql+= " and a.deliver_name like '%"+deliver_name+"%' ";
		}
		
		List<Map<String, Object>> list_sql = sqlMapper.public_list_all_sql(sql);
		
	    List<ManagementBean> list = ManagementBean.getBean(list_sql);
	    
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("序号","fin_supply_detail_id",0));
	    excel.add(new ExcelBean("结算单流水号","fin_supply_no",0));
	    excel.add(new ExcelBean("核算日期","jiesuan_time",0));
	    excel.add(new ExcelBean("订单日期","order_time",0));
	    excel.add(new ExcelBean("订单号","order_no",0));
	    excel.add(new ExcelBean("客户名称","kehu_company",0));
	    excel.add(new ExcelBean("配送员","deliver_name",0));
	    excel.add(new ExcelBean("业务员","salesman_name",0));
	    excel.add(new ExcelBean("大类","catalog_parent_name",0));
	    excel.add(new ExcelBean("子类","catalog_name",0));
	    excel.add(new ExcelBean("商品名称","item_name",0));
	    excel.add(new ExcelBean("规格","val_names",0));
	    excel.add(new ExcelBean("单位","unit",0));
	    excel.add(new ExcelBean("数量","actual_num",0));
	    excel.add(new ExcelBean("含税单价","customer_price",0));
	    excel.add(new ExcelBean("含税金额","money",0));
	    excel.add(new ExcelBean("销售属性","diff_type",0));
	    excel.add(new ExcelBean("客户备注","customer_note",0));
	    excel.add(new ExcelBean("采购单价","cost_price",0));
	    excel.add(new ExcelBean("采购成本","sum_cost",0));
	    excel.add(new ExcelBean("利润","profit",0));
	    excel.add(new ExcelBean("单据备注","hexiao_note",0));
	    excel.add(new ExcelBean("供应商编号","sid",0));
	    excel.add(new ExcelBean("供应商名称","company",0));
	    excel.add(new ExcelBean("单据状态","fuhe_status",0));
	    excel.add(new ExcelBean("复核日期","fuhe_time",0));
	    excel.add(new ExcelBean("复核人","fuhe_member_name",0));
	    map.put(0, excel);
	    String sheetName = export_name + "工作表";
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(ManagementBean.class, list, map, sheetName);
	    return xssfWorkbook;
	}
	
	

	//登记打印记录 member_id 打印人 print_type 打印类型 打印的单据：100送货单(订单oid)；200分拣单；201供应商计划表；202差异表；203采购单 ref_id 业务id description 描述
	public void print_set(int member_id, int print_type, int ref_id, int update_type) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		//description
		String description = "";
		
		String sql_member_name = "select member_name from hx_member where member_id = %s";
		String member_name = sqlMapper.public_one_scale(String.format(sql_member_name, member_id));
		description+= member_name +" 打印了 ";
		
		switch (print_type) {
		case 100:
			description+= "送货单";
			break;
		case 200:
			description+= "分拣单";
			break;
		case 201:
			description+= "供应商计划表";
			break;
		case 202:
			description+= "差异表";
			break;
		case 203:
			description+= "采购单";
			break;
		case 300:
			description+= "利润统计";
			break;
		case 301:
			description+= "利润统计月对比";
			break;
		case 400:
			description+= "实际订单";
			update_type = UpdateTypeEnum.print.getId();
			break;
		case 401:
			description+= "虚拟订单";
			update_type = UpdateTypeEnum.print.getId();
			break;
		case 500:
			description+= "实际订单";
			break;
		case 501:
			description+= "虚拟订单";
			break;
		case 600:
			description+= "订单通知";
			break;
		}
		
		String sql = "insert into hx_print (member_id, print_type,ref_id,update_type,description) values (%s,%s,%s,%s,'%s')";
		sqlMapper.insert(String.format(sql, member_id,print_type,ref_id,update_type,description));
		
	}
	
	//查询打印记录
	//print_type 打印类型 打印的单据：100送货单(订单oid)；200分拣单；201供应商计划表；202差异表；203采购单
	//默认最后十条记录
	public List<Map<String, Object>> print_get(int print_type) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		String sql = " select DATE_FORMAT(create_time,'%Y-%m-%d %H:%i') create_time, "
				+ "(select member_name from hx_member where member_id = hp.member_id ) member_name "
				+ " from hx_print hp where print_type = "+print_type+" order by hp.create_time desc ";
		
		List<Map<String, Object>> list = sqlMapper.public_list_page_sql(sql, 0, 10);
		
		return list;
	}

	/**
	 * 判断是否有过备份(如果没有就备份) 真实订单
	 * @throws IOException
	 * @throws SQLException
	 */
	public void if_first(int detail_oid) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		String sql_detail_log = "select count(1) from p_order_detail_log where detail_oid = %s";
		String detail_log_count = sqlMapper.public_one_scale(String.format(sql_detail_log, detail_oid));
		
		if (Integer.parseInt(detail_log_count) <= 0) {//第一次 先备份 在保存
			//备份p_order_detail
			int log_detail_id = copy_order_detail(detail_oid);
			//备份p_order_item
			copy_order_item(detail_oid, log_detail_id);
		}
		
	}
	
	/**
	 * 判断是否有过备份(如果没有就备份) 真实订单
	 * @throws IOException
	 * @throws SQLException
	 */
	public void if_first_real(int oid) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		String sql_detail_oid = "select detail_oid from p_order_detail where oid = %s and order_type = 1";
		int detail_oid = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_detail_oid, oid)));
		
		String sql_detail_log = "select count(1) from p_order_detail_log where detail_oid = %s";
		String detail_log_count = sqlMapper.public_one_scale(String.format(sql_detail_log, detail_oid));
		
		if (Integer.parseInt(detail_log_count) <= 0) {//第一次 先备份 在保存
			//备份p_order_detail
			int log_detail_id = copy_order_detail(detail_oid);
			//备份p_order_item
			copy_order_item(detail_oid, log_detail_id);
		}
		
	}
	
	/**
	 * 判断是否有过备份(如果没有就备份) 虚拟订单
	 * @throws IOException
	 * @throws SQLException
	 */
	public void if_first_virtual(int oid) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		String sql_detail_oid = "select detail_oid from p_order_detail where oid = %s and order_type = 2";
		int detail_oid = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_detail_oid, oid)));
		
		String sql_detail_log = "select count(1) from p_order_detail_log where detail_oid = %s";
		String detail_log_count = sqlMapper.public_one_scale(String.format(sql_detail_log, detail_oid));
		
		if (Integer.parseInt(detail_log_count) <= 0) {//第一次 先备份 在保存
			//备份p_order_detail
			int log_detail_id = copy_order_detail(detail_oid);
			//备份p_order_item
			copy_order_item(detail_oid, log_detail_id);
		}
		
	}
	
	/**
	 * 更新订单 备份订单 (真实订单)
	 * @param member_id
	 * @param detail_oid
	 * @throws IOException
	 * @throws SQLException
	 */
	public void update_copy_real(int member_id, int oid) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		String sql_detail_oid = "select detail_oid from p_order_detail where oid = %s and order_type = 1";
		int detail_oid = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_detail_oid, oid)));
		
		//更新实际订单详情 p_order_detail(重新计算真实订单详情统计)
		update_p_order_detail(member_id, detail_oid);
		//更新真实订单信息
		update_p_order_real(oid);
		
		//备份p_order_detail
		int log_detail_id = copy_order_detail(detail_oid);
		//备份p_order_item
		copy_order_item(detail_oid, log_detail_id);
		
	}
	
	/**
	 * 更新订单 备份订单 (虚拟订单)
	 * @param member_id
	 * @param detail_oid
	 * @throws IOException
	 * @throws SQLException
	 */
	public void update_copy_virtual(int member_id, int oid) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		String sql_detail_oid = "select detail_oid from p_order_detail where oid = %s and order_type = 2";
		int detail_oid = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_detail_oid, oid)));
		
		//更新实际订单详情 p_order_detail(重新计算真实订单详情统计)
		update_p_order_detail(member_id, detail_oid);
		//更新真实订单信息
		update_p_order_virtual(oid);
		
		
		//备份p_order_detail
		int log_detail_id = copy_order_detail(detail_oid);
		//备份p_order_item
		copy_order_item(detail_oid, log_detail_id);
		
	}

	/**
	 * 备份p_order_detail
	 * @param detail_oid
	 * @return
	 * @throws IOException
	 * @throws SQLException
	 */
	public int copy_order_detail(int detail_oid) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		int log_detail_id = -1;
		
		String sql = "insert into p_order_detail_log (detail_oid,oid,cost_amount,uniform_amount,customer_amount,pay_amount,item_num,update_member,update_note,order_type)"
				+ " select detail_oid,oid,cost_amount,uniform_amount,customer_amount,pay_amount,item_num,last_update_member,last_update_note,order_type"
				+ " from p_order_detail "
				+ " where detail_oid = %s ";
		sqlMapper.insert(String.format(sql, detail_oid));
		log_detail_id=Integer.parseInt(sqlMapper.selectOne("SELECT LAST_INSERT_ID() AS ID").get("ID").toString());
		
		return log_detail_id;
	}
	
	/**
	 * 备份p_order_item
	 * @param detail_oid
	 * @param log_detail_id
	 * @throws IOException
	 * @throws SQLException
	 */
	public void copy_order_item(int detail_oid, int log_detail_id) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		String sql = "insert into p_order_item_log (log_detail_id,order_item_id,detail_oid,item_id,sku_id,order_num,actual_num,cost_price,uniform_price, "
				+ " customer_price,pay_price,money,sum_cost,customer_note,item_name,item_small_img,val_names,unit,source_type,source_id,update_note) "
				+ " select %s,order_item_id,detail_oid,item_id,sku_id,order_num,actual_num,cost_price,uniform_price, "
				+ " customer_price,pay_price,money,sum_cost,customer_note,item_name,item_small_img,val_names,unit,source_type,source_id,last_update_note "
				+ " from p_order_item where detail_oid = %s ";
		sqlMapper.insert(String.format(sql, log_detail_id,detail_oid));
	}
	
	/**
	 * 更新订单真实总金额p_order
	 * @param oid
	 * @throws IOException
	 * @throws SQLException
	 */
	public void update_p_order_real(int oid) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		String sql_update_order = "update p_order set real_pay_amount = (select pay_amount from p_order_detail where oid = %s and order_type = 1) "
				+ " , diff_amount = ((select pay_amount from p_order_detail where oid = %s and order_type = 1) - "
				+ "(select pay_amount from p_order_detail where oid = %s and order_type = 2) + charge_amount) "
				+ " , update_time = now() where oid = %s ";
		sqlMapper.update(String.format(sql_update_order, oid,oid,oid,oid));
	}
	
	/**
	 * 更新虚拟订单总金额p_order
	 * @param oid
	 * @throws IOException
	 * @throws SQLException
	 */
	public void update_p_order_virtual(int oid) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		String sql_update_order = "update p_order set virtual_pay_amount = (select pay_amount from p_order_detail where oid = %s and order_type = 2) "
				+ " , diff_amount = ((select pay_amount from p_order_detail where oid = %s and order_type = 1) -"
				+ " (select pay_amount from p_order_detail where oid = %s and order_type = 2) + charge_amount)"
				+ " , update_time = now()  where oid = %s ";
		sqlMapper.update(String.format(sql_update_order, oid,oid,oid,oid));
	}
	
	/**
	 * 更新订单相关数据
	 * @param member_id
	 * @param detail_oid
	 * @throws IOException
	 * @throws SQLException
	 */
	public void update_p_order_detail(int member_id, int detail_oid) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		String sql_update_order_detail = "update p_order_detail pod set "
				+ "   cost_amount = (select sum(actual_num*cost_price) from p_order_item where detail_oid = pod.detail_oid)"
				+ " , uniform_amount = (select sum(actual_num*uniform_price) from p_order_item where detail_oid = pod.detail_oid) "
				+ " , customer_amount = (select sum(actual_num*customer_price) from p_order_item where detail_oid = pod.detail_oid) "
				+ " , pay_amount = (select sum(actual_num*pay_price) from p_order_item where detail_oid = pod.detail_oid) "
				+ " , item_num = (select sum(actual_num) from p_order_item where detail_oid = pod.detail_oid) "
				+ " , last_update_note = (select group_concat(last_update_note) from p_order_item where detail_oid = pod.detail_oid) "
				+ " , last_update_member = %s "
				+ " where pod.detail_oid = %s ";
		sqlMapper.update(String.format(sql_update_order_detail, member_id,detail_oid));
	}
	
	/**
	 * 合计制单打印
	 * @param oid
	 * @throws IOException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> total_print(int oid,int order_type) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		//客户名称和日期
		String sql_customer_name = "select company, DATE(send_time) send_time from p_order where oid = %s";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql_customer_name, oid));
		
		int actual_nums = 0;
		BigDecimal sum_moneys = new BigDecimal(0);
		
		//订单商品
		String sql = "select pca.pid,pca.catalog_id, poi.item_name,poi.val_names, poi.unit,"
				+ " poi.customer_note, poi.actual_num, poi.customer_price, (poi.actual_num*poi.customer_price) sum_money "
				+ " from p_order_item poi "
				+ " left join p_order_detail pod on poi.detail_oid = pod.detail_oid "
				+ " left join p_item pi on poi.item_id = pi.item_id "
				+ " left join p_catalog pca on pi.catalog_id = pca.catalog_id "
				+ " where pod.oid = %s and pod.order_type = %s "
				+ " order by pca.pid, pca.catalog_id, poi.order_item_id ";
		List<Map<String, Object>> list_item = sqlMapper.public_list_all_sql(String.format(sql, oid,order_type));
		
		for (Map<String, Object> map_item : list_item) {
			actual_nums+= Integer.valueOf(map_item.get("actual_num").toString());
			sum_moneys = sum_moneys.add(new BigDecimal(map_item.get("sum_money").toString()));
		}
		
		map.put("list", list_item);
		map.put("actual_nums", actual_nums);
		map.put("sum_moneys", sum_moneys);
		//获取下单用户品牌
		String sql_brand = "select (case brand_type "
				+ " when 0 then (select brand_name from p_brand where brand_type = 1) "
				+ " when 1 then (select brand_name from p_brand where brand_type = 1) "
				+ " when 2 then (select brand_name from p_brand where brand_type = 2) end "
				+ " ) brand_type from p_user where uid = (select uid from p_customer where cid = (select cid from p_order where oid = %s)) ";
		String brand_type = sqlMapper.public_one_scale(String.format(sql_brand, oid));
		map.put("brand_type", brand_type);
		
		List<Map<String, Object>> list = new ArrayList<>();
		list.add(map);
		
		return list;
	}
	
	/**
	 * 分类制单打印
	 * @param oid
	 * @throws IOException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> classes_print(int oid,int order_type) throws IOException, SQLException {
        SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
        List<Map<String, Object>> list = null;
        
		//大分类id
		String sql_catalog_id = "select pca.pid from p_order_item poi "
				+ " left join p_order_detail pod on poi.detail_oid = pod.detail_oid "
				+ " left join p_item pi on poi.item_id = pi.item_id "
				+ " left join p_catalog pca on pi.catalog_id = pca.catalog_id "
				+ " where pod.oid = %s and pod.order_type = %s group by pca.pid ";
		List<Map<String, Object>> list_pid = sqlMapper.public_list_all_sql(String.format(sql_catalog_id, oid, order_type));
		
		
		for (Map<String, Object> map_pid : list_pid) {
			list = new ArrayList<>();
			
			int actual_nums = 0;
			BigDecimal sum_moneys = new BigDecimal(0);
			
			//客户名称和日期
			String sql_customer_name = "select company, DATE(send_time) send_time from p_order where oid = %s";
			Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql_customer_name, oid));
			
			//订单商品
			String sql = "select pca.pid,pca.catalog_id, poi.item_name,poi.val_names, poi.unit, "
					+ " poi.customer_note, poi.actual_num, poi.customer_price, (poi.actual_num*poi.customer_price) sum_money "
					+ " from p_order_item poi "
					+ " left join p_order_detail pod on poi.detail_oid = pod.detail_oid "
					+ " left join p_item pi on poi.item_id = pi.item_id "
					+ " left join p_catalog pca on pi.catalog_id = pca.catalog_id "
					+ " where pod.oid = %s and pod.order_type = %s and pca.pid = %s "
					+ " order by pca.pid, pca.catalog_id, poi.order_item_id ";
			List<Map<String, Object>> list_item = sqlMapper.public_list_all_sql(String.format(sql, oid,order_type,map_pid.get("pid")));
			
			for (Map<String, Object> map_item : list_item) {
				Object actual_num = map_item.get("actual_num");
				Object sum_money = map_item.get("sum_money");
				if (actual_num != null && !actual_num.equals("")) {
					actual_nums+= Integer.valueOf(map_item.get("actual_num").toString());
				}
				if (sum_money != null && !sum_money.equals("")) {
					sum_moneys = sum_moneys.add(new BigDecimal(map_item.get("sum_money").toString()));
				}
			}
			
			map.put("list", list_item);
			map.put("actual_nums", actual_nums);
			map.put("sum_moneys", sum_moneys);
			
			//获取下单用户品牌
			String sql_brand = "select (case brand_type "
					+ " when 0 then (select brand_name from p_brand where brand_type = 1) "
					+ " when 1 then (select brand_name from p_brand where brand_type = 1) "
					+ " when 2 then (select brand_name from p_brand where brand_type = 2) end "
					+ " ) brand_type from p_user where uid = (select uid from p_customer where cid = (select cid from p_order where oid = %s)) ";
			String brand_type = sqlMapper.public_one_scale(String.format(sql_brand, oid));
			map.put("brand_type", brand_type);
			
			list.add(map);
		}
		
		return list;
	}
	
	/**
	 * 顺序制单打印
	 * @param oid
	 * @throws IOException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> order_print(int oid,int order_type) throws IOException, SQLException {
        SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		//客户名称和日期
		String sql_customer_name = "select company, DATE(send_time) send_time from p_order where oid = %s";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql_customer_name, oid));
		
		int actual_nums = 0;
		BigDecimal sum_moneys = new BigDecimal(0);
		
		//订单商品
		String sql = "select pca.pid,pca.catalog_id, poi.item_name,poi.val_names, poi.unit,"
				+ " poi.customer_note, poi.actual_num, poi.customer_price, (poi.actual_num*poi.customer_price) sum_money "
				+ " from p_order_item poi "
				+ " left join p_order_detail pod on poi.detail_oid = pod.detail_oid "
				+ " left join p_item pi on poi.item_id = pi.item_id "
				+ " left join p_catalog pca on pi.catalog_id = pca.catalog_id "
				+ " where pod.oid = %s and pod.order_type = %s "
				+ " order by poi.order_item_id ";
		List<Map<String, Object>> list_item = sqlMapper.public_list_all_sql(String.format(sql, oid, order_type));
		
		for (Map<String, Object> map_item : list_item) {
			actual_nums+= Integer.valueOf(map_item.get("actual_num").toString());
			sum_moneys = sum_moneys.add(new BigDecimal(map_item.get("sum_money").toString()));
		}
		
		map.put("list", list_item);
		map.put("actual_nums", actual_nums);
		map.put("sum_moneys", sum_moneys);
		
		//获取下单用户品牌
		String sql_brand = "select (case brand_type "
				+ " when 0 then (select brand_name from p_brand where brand_type = 1) "
				+ " when 1 then (select brand_name from p_brand where brand_type = 1) "
				+ " when 2 then (select brand_name from p_brand where brand_type = 2) end "
				+ " ) brand_type from p_user where uid = (select uid from p_customer where cid = (select cid from p_order where oid = %s)) ";
		String brand_type = sqlMapper.public_one_scale(String.format(sql_brand, oid));
		map.put("brand_type", brand_type);
		
		List<Map<String, Object>> list = new ArrayList<>();
		list.add(map);
		
		return list;
	}
	
	/**
	 * 判断价格是否有被修改 1是2否
	 * @throws IOException
	 * @throws SQLException
	 */
	public int is_cost_price(int order_item_id, BigDecimal cost_price) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		int i = 2; //1是2否
		
		String sql_cost_price = "select cost_price from p_order_item where order_item_id = %s";
		String cost_price_str = sqlMapper.public_one_scale(String.format(sql_cost_price, order_item_id));
		
		BigDecimal cost_prce_old = new BigDecimal(cost_price_str);
		
		if(!(cost_prce_old.compareTo(cost_price) == 0)) {
			i = 1;
		}
		
		return i;
	}
	
	
	/**
	 * 获取修改
	 * @return
	 * @throws IOException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> update_remarks(int print_type) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		//记录
		String sql_remarks = "select DATE_FORMAT(hp.create_time,'%%Y-%%m-%%d %%H:%%i:%%m') create_time, "
				+ " (select member_name from hx_member where member_id = hp.member_id ) member_name ,description "
				+ "  from hx_print hp where hp.print_type = %s order by hp.create_time desc limit 10 ";
		List<Map<String, Object>> list_remarks = sqlMapper.public_list_all_sql(String.format(sql_remarks, print_type));
		
		return list_remarks;
	}
	
	/**
	 * 推送(utype 1 用户端 2 商家端)
	 * @param uid
	 * @param msg_type
	 * @param msg_value
	 * @throws IOException
	 * @throws SQLException
	 */
	public void send_message(int msg_type, String msg_value, int utype, int brand_id, String msg_title) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		Map<String, Object> map= new HashMap<>();
		map.put("type", msg_type);
		map.put("value", msg_value);

		String sql_limit = "select count(1) from p_user where device_tokens is not null and brand_type = %s and  utype = %s";
		int limit = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_limit, brand_id,utype)));
		
		int page = 0;
		
		page = limit/500;
		if((limit%500)>0) {
			page+=1;
		}
		
		for (int i = 1; i <= page; i++) {
			String sql = "select group_concat(device_tokens), utype "
					+ "from p_user where device_tokens is not null and brand_type = %s and utype = %s order by uid limit "+(page-1)*500+", "+500+"";
			String device_tokens = sqlMapper.public_one_scale(String.format(sql, brand_id , utype));
			
			String title = "通知";
			String content = "通知";
			try
			{
				if(device_tokens != null && !device_tokens.isEmpty())
				{
					if(device_tokens.length() == 64)
					{
						UPushUtils.sendIOSUnicast(utype, device_tokens, "通知", msg_title, content, map);
					}
					else
					{
						UPushUtils.sendAndroidUnicast(utype, device_tokens, "通知", msg_title, content, map);
					}
				}
				
				results.setMessage("提交成功");//输出：提示信息
				results.setStatus(1);//输出：状态1成功 0失败
			}
			catch(Exception e)
			{
				System.out.println("推送失败");
				e.printStackTrace();
				
				results.setMessage("推送失败");//输出：提示信息
				results.setStatus(0);//输出：状态1成功 0失败
			}
			
		}
	}
	
	/**
	 * 推送
	 * @param uid
	 * @param msg_type
	 * @param msg_value
	 * @throws IOException
	 * @throws SQLException
	 */
	public void send_message(int msg_type, String msg_value, int uid, String title, String content) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		Map<String, Object> map= new HashMap<>();
		map.put("type", msg_type);
		map.put("value", msg_value);
		
		Map<String, Object> mapUser = sqlMapper.public_one_detail("select device_tokens, utype from p_user where uid=" + uid);
		String device_tokens = mapUser.get("device_tokens").toString();
		int utype = Integer.parseInt( mapUser.get("utype").toString() );
		
		try
		{
			if(device_tokens != null && !device_tokens.isEmpty())
			{
				if(device_tokens.length() == 64)
				{
					UPushUtils.sendIOSUnicast(utype, device_tokens, "通知", title, content, map);
				}
				else
				{
					UPushUtils.sendAndroidUnicast(utype, device_tokens, "通知", title, content, map);
				}
			}
			
			results.setMessage("提交成功");//输出：提示信息
			results.setStatus(1);//输出：状态1成功 0失败
		}
		catch(Exception e)
		{
			System.out.println("推送失败");
			e.printStackTrace();
			
			results.setMessage("推送失败");//输出：提示信息
			results.setStatus(0);//输出：状态1成功 0失败
		}
	}
	
	/**
	 * 订单修改通知
	 * @param oid
	 * @throws IOException
	 * @throws SQLException
	 */
	public void order_update_notice(int oid) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		//客户通知
		String sql_customer = "select uid from p_customer where cid = (select cid from p_order where oid = %s)";
		int uid = Integer.valueOf(sqlMapper.public_one_scale(String.format(sql_customer, oid)));
		
		//订单编号
		String sql_order_no = "select order_no from p_order where oid = %s";
		String order_no = sqlMapper.public_one_scale(String.format(sql_order_no, oid));
		
		String msg_value = "您的编号为 "+order_no+" 的订单已被修改,请及时查看";
		
		String title = "订单修改";
		String content = "您的编号为 "+order_no+" 的订单已被修改,请及时查看";
		send_message(MsgTypeEnum.client_order.getId(), msg_value, uid, title, content);
		
		
		//供应商通知
		//查询供应商id
		String sql = "select poi.source_id from p_order_item poi  "
				+ " inner join p_order_detail pod on pod.detail_oid = poi.detail_oid "
				+ " where pod.order_type = 1 and poi.source_type = 2 and pod.oid = %s "
				+ " group by poi.source_id order by poi.source_id ";
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sql, oid));
		
		for (Map<String, Object> map : list) {
			int source_id = Integer.valueOf(map.get("source_id").toString());
			send_message(MsgTypeEnum.system_order.getId(), msg_value,source_id,title,content);
		}
		
	}
	
	/**
	 * 更新订单差异
	 * @param oid
	 * @throws IOException
	 * @throws SQLException
	 */
	public void update_order_diff(int oid) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		
		//判断有没有虚拟订单
		String sql = "select count(1) from p_order_detail where oid = "+oid+" and order_type = 2";
		int count = Integer.valueOf(sqlMapper.public_one_scale(sql));
		if (count > 0) {
			//删除订单之前的差异
			String sql_delete_order_diff = "delete from p_order_diff where oid = %s";
			sqlMapper.delete(String.format(sql_delete_order_diff, oid));
			
			//已比较过的虚拟订单order_item_id 集合
			List<Integer> list_order_item_id_virtual = new ArrayList<>();
			
			String sql_real = "select order_item_id, item_id, sku_id, sum(actual_num) actual_num from p_order_item poi "
					+ " where poi.detail_oid = "
					+ "(select pod.detail_oid from p_order_detail pod where pod.oid = %s and pod.order_type = 1 ) "
					+ " group by detail_oid, item_id, sku_id ";
			List<Map<String, Object>> list_real = sqlMapper.public_list_all_sql(String.format(sql_real, oid));
			//实际订单方面
			for (Map<String, Object> map_real : list_real) {
				int actual_num_real = Integer.valueOf(map_real.get("actual_num").toString());
				int order_item_id_real = Integer.valueOf(map_real.get("order_item_id").toString());
				int item_id = Integer.valueOf(map_real.get("item_id").toString());
				int sku_id = Integer.valueOf(map_real.get("sku_id").toString());
						
				String sql_virtual = "select order_item_id, sum(actual_num) actual_num from p_order_item "
						+ "where item_id = %s and sku_id = %s and detail_oid = "
						+ "(select pod.detail_oid from p_order_detail pod where pod.oid = %s and pod.order_type = 2) "
						+ " group by detail_oid, item_id, sku_id ";
				Map<String, Object> map_virtual = sqlMapper.public_one_detail(String.format(sql_virtual, item_id,sku_id,oid));
				if (map_virtual != null && map_virtual.size() > 0) { //有相应的虚拟订单
					int actual_num_virtual = Integer.valueOf(map_virtual.get("actual_num").toString());
					int order_item_id_virtual = Integer.valueOf(map_virtual.get("order_item_id").toString());
					//差额
					int diff_num = actual_num_real - actual_num_virtual;
					if (diff_num != 0 && diff_num > 0) {
						int diff_type = OiTypeEnum.delivery.getId();
						insert_diff(oid, diff_type, diff_num,order_item_id_virtual);
					} else {
						diff_num = Math.abs(diff_num);
						int diff_type = OiTypeEnum.single.getId();
						insert_diff(oid, diff_type, diff_num,order_item_id_virtual);
					}
					
					//查出已处理的order_item_id
					String sql_virtual_order_item_id = "select order_item_id from p_order_item "
							+ "where item_id = %s and sku_id = %s and detail_oid = "
							+ "(select pod.detail_oid from p_order_detail pod where pod.oid = %s and pod.order_type = 2) ";
					List<Map<String, Object>> list_virtual_order_item_id = sqlMapper.public_list_all_sql
							(String.format(sql_virtual_order_item_id, item_id,sku_id,oid));
					
					for (Map<String, Object> map : list_virtual_order_item_id) {
						list_order_item_id_virtual.add(Integer.valueOf(map.get("order_item_id").toString()));
					}
					
				} else {//没有相应的虚拟订单
					int diff_type = OiTypeEnum.delivery.getId();
					insert_diff(oid, diff_type, actual_num_real,order_item_id_real);
				}
			}
			
			if (list_order_item_id_virtual != null && list_order_item_id_virtual.size() > 0) {
				String order_item_id_str = StringUtils.join(list_order_item_id_virtual, ",");
				
				int diff_type = OiTypeEnum.single.getId();
				
				//虚拟订单方面 查出没比对过的数据直接插入
				String sql_insert_diff = "insert into p_order_diff ( oid,diff_type, diff_num, item_id, sku_id, cost_price,"
						+ " uniform_price, customer_price, pay_price,item_name,val_names, item_img, unit, customer_note) "
						+ " select %s,%s,actual_num, item_id, sku_id, cost_price, uniform_price, customer_price, pay_price,item_name,"
						+ " val_names,item_small_img, unit, customer_note from p_order_item where order_item_id not in (%s) and detail_oid =  "
						+ "(select pod.detail_oid from p_order_detail pod where pod.oid = %s and pod.order_type = 1 )"
						+ " group by detail_oid, item_id, sku_id ";
				sqlMapper.insert(String.format(sql_insert_diff, oid,diff_type,order_item_id_str,oid));
			}
			
		}
		//无虚拟订单 不处理
		
	}
	
	/**
	 * 差异表新增数据
	 * @param oid
	 * @param diff_type
	 * @param diff_num
	 * @throws IOException
	 * @throws SQLException
	 */
	public void insert_diff(int oid, int diff_type, int diff_num, int order_item_id) throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		String sql_insert_diff = "insert into p_order_diff ( oid,diff_type, diff_num, item_id, sku_id, cost_price,"
				+ " uniform_price, customer_price, pay_price,item_name,val_names, item_img, unit,customer_note) "
				+ " select %s,%s,%s, item_id, sku_id, cost_price, uniform_price, customer_price, pay_price,item_name,"
				+ " val_names,item_small_img, unit, customer_note from p_order_item where order_item_id = %s";
		sqlMapper.insert(String.format(sql_insert_diff, oid,diff_type,diff_num,order_item_id));
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
